我正在尝试将我的应用程序与Twitter集成。但是我收到了一些错误。我使用了this site中的代码。我为我的应用创建了一个API。我已将网站的网址设为http://www.androidhive.info,将回调网址设为http://androidhive.info。我更新了我的消费者和消费者密钥。
我不确定为什么会收到此错误。我在这里附加了错误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)
答案 0 :(得分:3)
android.os.NetworkOnMainThreadException
在Android 3.0中,您无法尝试从主(UI)线程执行网络操作。因此,您必须将代码移至后台线程,最佳解决方案是使用AsyncTask
来实现目标。
它被指定为您的任务。在UI线程上做一些长计算,网络和其他东西通常也是一种不好的做法。您应该让Activity类清理并将应用程序逻辑从外观中分离出来。
AsyncTask的主要优点:它是通用类型(类型安全),如果你想宣布一个用户“某些东西被改变了”并且实现了,那么它提供了一些方法很难理解。
如果您需要一些灵感,请查看Zwitscher's github。
答案 1 :(得分:0)
在主线程上执行网络操作是不好的做法,这就是你看到这个的原因。政策阻止了这一点。 如果您真的必须进行测试,请将以下内容放入OnCreate:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
请注意,执行此操作是非常糟糕的做法,理想情况下应将您的网络代码移至AsyncTask
或Thread
。
答案 2 :(得分:0)
您应该将尝试使用Twitter API(或执行任何其他网络操作)的代码移动到新的AsyncTask
或新的Thread
。