我想使用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)
應我把整個登錄方法的AsyncTask:
嘗試通過如下實施
AsyncTask
把你的代碼doInBackground()
方法? – user3851588@ user3851588,是的。 –
無法訪問this.startactivity方法! :( – user3851588