2014-07-18 154 views
0

我想使用Twitter4j庫登錄到我的Twitter應用程序。但是每當我點擊登錄按鈕時,我的應用程序就會崩潰。Twitter Android登錄錯誤使用twitter4j庫

這是我從我的類方法 -

btnLoginTwitter.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // Call login twitter function 
      loginToTwitter(); 
     } 
    }); 


/** 
* Function to login twitter 
* */ 
private void loginToTwitter() { 
    // Check if already logged in 
    if (!isTwitterLoggedInAlready()) { 
     ConfigurationBuilder builder = new ConfigurationBuilder(); 
     builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); 
     builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); 
     Configuration configuration = builder.build(); 

     TwitterFactory factory = new TwitterFactory(configuration); 
     twitter = factory.getInstance(); 

     try { 
      requestToken = twitter 
        .getOAuthRequestToken(TWITTER_CALLBACK_URL); 
      this.startActivity(new Intent(Intent.ACTION_VIEW, Uri 
        .parse(requestToken.getAuthenticationURL()))); 
     } catch (TwitterException e) { 
      e.printStackTrace(); 
     } 
    } else { 
     // user already logged into twitter 
     Toast.makeText(getApplicationContext(), 
       "Already Logged into twitter", Toast.LENGTH_LONG).show(); 
    } 
} 

當我按下按鈕登錄它顯示了以下的logcat -

07-18 00:55:00.726: E/AndroidRuntime(1185): FATAL EXCEPTION: main 
07-18 00:55:00.726: E/AndroidRuntime(1185): android.os.NetworkOnMainThreadException 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at java.net.InetAddress.getAllByName(InetAddress.java:214) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:311) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:158) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:121) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:276) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at com.androidhive.twitterconnect.MainActivity.loginToTwitter(MainActivity.java:236) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at com.androidhive.twitterconnect.MainActivity.access$1(MainActivity.java:223) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at com.androidhive.twitterconnect.MainActivity$1.onClick(MainActivity.java:123) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at android.view.View.performClick(View.java:4240) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at android.view.View$PerformClick.run(View.java:17721) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at android.os.Handler.handleCallback(Handler.java:730) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at android.os.Handler.dispatchMessage(Handler.java:92) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at android.os.Looper.loop(Looper.java:137) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at java.lang.reflect.Method.invoke(Method.java:525) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
07-18 00:55:00.726: E/AndroidRuntime(1185):  at dalvik.system.NativeStart.main(Native Method) 

回答

1

「android.os.NetworkOnMainThreadException」意味着你正在執行網絡相關功能像Http的帖子,得到等等在你的主GUI線程..你所要做的就是把這東西放在AsyncTask

做這樣的事情:

private void loginToTwitter() 
{ 
new AsyncTask<Void, Void, Boolean>() { 
@Override 
protected void onPreExecute() { 
       super.onPreExecute(); 
} 

@Override 
protected Boolean doInBackground(Void... params) { 
if (!isTwitterLoggedInAlready()) { 
ConfigurationBuilder builder = new ConfigurationBuilder(); 
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); 
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); 
Configuration configuration = builder.build(); 

TwitterFactory factory = new TwitterFactory(configuration); 
twitter = factory.getInstance(); 

try { 
requestToken = twitter 
      .getOAuthRequestToken(TWITTER_CALLBACK_URL); 
return true; 
} catch (TwitterException e) { 
e.printStackTrace(); 
} 
} else { 
return false; 
} 
} 

@Override 
protected void onPostExecute(Boolean result) { 
super.onPostExecute(result); 

if(result) 
{ 
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri 
        .parse(requestToken.getAuthenticationURL()))); 
}else{ 
Toast.makeText(getApplicationContext(), 
       "Already Logged into twitter", Toast.LENGTH_LONG).show();} 
} 
}.execute(); 
} 

注意:你不能從doInBackgroung()訪問GUI線程你可以從preExecutepostExecute方法

+0

應我把整個登錄方法的AsyncTask:

嘗試通過如下實施AsyncTask把你的代碼doInBackground()方法? – user3851588

+0

@ user3851588,是的。 –

+0

無法訪問this.startactivity方法! :( – user3851588

0

您正在執行耗時的應用程序的主線程任務訪問GUI線程。這就是爲什麼它給android.os.NetworkOnMainThreadException錯誤。

private class WSCalling extends AsyncTask<Void, Void, Integer> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected Integer doInBackground(Void... params) { 
     if (!isTwitterLoggedInAlready()) { 
    ConfigurationBuilder builder = new ConfigurationBuilder(); 
    builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); 
    builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); 
    Configuration configuration = builder.build(); 

    TwitterFactory factory = new TwitterFactory(configuration); 
    twitter = factory.getInstance(); 

    try { 
     requestToken = twitter 
       .getOAuthRequestToken(TWITTER_CALLBACK_URL); 

    } catch (TwitterException e) { 
     e.printStackTrace(); 
     } 
    } 
     return requestToken; 
    } 

    @Override 
    protected void onPostExecute(Integer args) { 
      if(args != null) 
     { 
     this.startActivity(new Intent(Intent.ACTION_VIEW, Uri 
       .parse(args.getAuthenticationURL()))); 
     }else { 
    // user already logged into twitter 
    Toast.makeText(getApplicationContext(), 
      "Already Logged into twitter", Toast.LENGTH_LONG).show(); 
    } 
    } 
}