我正在嘗試將我的應用程序與Twitter集成。但是我收到了一些錯誤。我使用this site的代碼。我爲我的應用程序創建了一個API。我已將網站的網址設置爲http://www.androidhive.info,並將回調網址設置爲http://androidhive.info。我更新了消費者和消費者的關鍵祕密。將Twitter與我的Android應用程序集成
我不知道爲什麼我得到這個錯誤。我在這裏附加了錯誤logcat:
03-18 21:36:13.971: E/AndroidRuntime(1339): FATAL EXCEPTION: main
03-18 21:36:13.971: E/AndroidRuntime(1339): android.os.NetworkOnMainThreadException
03-18 21:36:13.971: E/AndroidRuntime(1339): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
03-18 21:36:13.971: E/AndroidRuntime(1339): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
03-18 21:36:13.971: E/AndroidRuntime(1339): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
03-18 21:36:13.971: E/AndroidRuntime(1339): at java.net.InetAddress.getAllByName(InetAddress.java:220)
03-18 21:36:13.971: E/AndroidRuntime(1339): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
03-18 21:36:13.971: E/AndroidRuntime(1339): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-18 21:36:13.971: E/AndroidRuntime(1339): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
03-18 21:36:13.971: E/AndroidRuntime(1339): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
03-18 21:36:13.971: E/AndroidRuntime(1339): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-18 21:36:13.971: E/AndroidRuntime(1339): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
03-18 21:36:13.971: E/AndroidRuntime(1339): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
03-18 21:36:13.971: E/AndroidRuntime(1339): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
03-18 21:36:13.971: E/AndroidRuntime(1339): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
03-18 21:36:13.971: E/AndroidRuntime(1339): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
03-18 21:36:13.971: E/AndroidRuntime(1339): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
03-18 21:36:13.971: E/AndroidRuntime(1339): at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:158)
03-18 21:36:13.971: E/AndroidRuntime(1339): at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
03-18 21:36:13.971: E/AndroidRuntime(1339): at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
03-18 21:36:13.971: E/AndroidRuntime(1339): at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:121)
03-18 21:36:13.971: E/AndroidRuntime(1339): at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
03-18 21:36:13.971: E/AndroidRuntime(1339): at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:276)
03-18 21:36:13.971: E/AndroidRuntime(1339): at com.androidhive.twitterconnect.MainActivity.loginToTwitter(MainActivity.java:236)
03-18 21:36:13.971: E/AndroidRuntime(1339): at com.androidhive.twitterconnect.MainActivity.access$1(MainActivity.java:223)
03-18 21:36:13.971: E/AndroidRuntime(1339): at com.androidhive.twitterconnect.MainActivity$1.onClick(MainActivity.java:123)
03-18 21:36:13.971: E/AndroidRuntime(1339): at android.view.View.performClick(View.java:3480)
03-18 21:36:13.971: E/AndroidRuntime(1339): at android.view.View$PerformClick.run(View.java:13983)
03-18 21:36:13.971: E/AndroidRuntime(1339): at android.os.Handler.handleCallback(Handler.java:605)
03-18 21:36:13.971: E/AndroidRuntime(1339): at android.os.Handler.dispatchMessage(Handler.java:92)
03-18 21:36:13.971: E/AndroidRuntime(1339): at android.os.Looper.loop(Looper.java:137)
03-18 21:36:13.971: E/AndroidRuntime(1339): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-18 21:36:13.971: E/AndroidRuntime(1339): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 21:36:13.971: E/AndroidRuntime(1339): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 21:36:13.971: E/AndroidRuntime(1339): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-18 21:36:13.971: E/AndroidRuntime(1339): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-18 21:36:13.971: E/AndroidRuntime(1339): at dalvik.system.NativeStart.main(Native Method)
請問您可以發佈使用Twitter API的好做法... – Developer 2013-03-18 16:05:03
任何人都可以發佈代碼糾正,因爲我沒有任何使用twitter,facebook ..... api的經驗。 – Developer 2013-03-18 16:35:15
如果你只是想讓它快速工作,請將我上面的答案中的兩行復制到主要活動的onCreate()方法中,但請仔細閱讀爲什麼它不好。 – tristan2468 2013-03-18 16:47:53