2

我正在尝试将我的应用程序与 Twitter 集成。但是我遇到了一些错误。我已经使用了这个站点的代码。我为我的应用创建了一个 API。我已将网站的 URL 设置为http://www.androidhive.info并将回调 URL 设置为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)
4

3 回答 3

3

android.os.NetworkOnMainThreadException

From Android 3.0 you can't attempt to perform networking operation from Main(UI) Thread. So you have to move your code into background Thread and best solution is an usage of AsyncTask to reach your goal.

It's designated for tasks as yours. Also generally is a bad practise to make some long calculations, networking and other stuff on UI Thread. You should let your Activity classes clean and separate logic of application from an appearance.

Main advantages of AsyncTask: it's generic type(type safe), offers a few methods if you want to announce an user that "something is changed" and implementation isn't difficult to understand.

If you want some inspiration look at Zwitscher's github.

于 2013-03-18T16:02:57.697 回答
0

在主线程上执行网络操作是不好的做法,这就是你看到这个的原因。它被政策所阻止。如果您确实必须这样做以进行测试,请将以下内容放入您的 OnCreate:

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 

请记住,这样做是非常糟糕的做法,理想情况下应该将您的网络代码移动到一个AsyncTask或一个Thread.

于 2013-03-18T15:47:47.393 回答
0

您应该将尝试使用 Twitter API(或执行任何其他网络操作)的代码移至新的AsyncTask或新的Thread.

于 2013-03-18T15:58:06.580 回答