这是Logcat的整个堆栈。我基本上使用的是twitter4j库。我已经从twitter开发网站添加了我的令牌和秘密。 你可以帮我调试吗? 代码主要基于http://www.androidhive.info/2012/09/android-twitter-oauth-connect-tutorial/
12-13 16:38:22.225: E/AndroidRuntime(12659): FATAL EXCEPTION: main
12-13 16:38:22.225: E/AndroidRuntime(12659): Process: code.laerstudios.gappa, PID: 12659
12-13 16:38:22.225: E/AndroidRuntime(12659): android.os.NetworkOnMainThreadException
12-13 16:38:22.225: E/AndroidRuntime(12659):at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
12-13 16:38:22.225: E/AndroidRuntime(12659): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-13 16:38:22.225: E/AndroidRuntime(12659): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-13 16:38:22.225: E/AndroidRuntime(12659): at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
12-13 16:38:22.225: E/AndroidRuntime(12659): at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:135)
12-13 16:38:22.225: E/AndroidRuntime(12659): at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
12-13 16:38:22.225: E/AndroidRuntime(12659): at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:98)
12-13 16:38:22.225: E/AndroidRuntime(12659): at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:122)
12-13 16:38:22.225: E/AndroidRuntime(12659): at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
12-13 16:38:22.225: E/AndroidRuntime(12659): at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:298)
12-13 16:38:22.225: E/AndroidRuntime(12659): at code.laerstudios.gappa.MainActivity.loginToTwitter(MainActivity.java:225)
12-13 16:38:22.225: E/AndroidRuntime(12659): at code.laerstudios.gappa.MainActivity.access$0(MainActivity.java:212)
12-13 16:38:22.225: E/AndroidRuntime(12659): at code.laerstudios.gappa.MainActivity$1.onClick(MainActivity.java:153)
12-13 16:38:22.225: E/AndroidRuntime(12659): at android.view.View.performClick(View.java:4438)
12-13 16:38:22.225: E/AndroidRuntime(12659): at android.view.View$PerformClick.run(View.java:18422)
12-13 16:38:22.225: E/AndroidRuntime(12659): at android.os.Handler.handleCallback(Handler.java:733)
12-13 16:38:22.225: E/AndroidRuntime(12659): at android.os.Handler.dispatchMessage(Handler.java:95)
12-13 16:38:22.225: E/AndroidRuntime(12659): at android.os.Looper.loop(Looper.java:136)
12-13 16:38:22.225: E/AndroidRuntime(12659): at android.app.ActivityThread.main(ActivityThread.java:5019)
12-13 16:38:22.225: E/AndroidRuntime(12659): at java.lang.reflect.Method.invoke(Native Method)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-13 16:38:22.225: E/AndroidRuntime(12659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-13 16:38:25.449: I/Process(12659): Sending signal. PID: 12659 SIG: 9
答案 0 :(得分:1)
教导一个人捕鱼;
如果您获得Google的例外,您会注意到您无法在主线程上运行网络操作。
答案 1 :(得分:0)
顶部的例外很清楚:android.os.NetworkOnMainThreadException
Android不允许您在主线程上执行网络操作。尝试创建AsyncTask
并在那里进行异步操作。
答案 2 :(得分:0)
简单你在UI
线程中有一些与网络相关的代码,所以把它放在像
Thread thread = new Thread()
{
@Override
public void run() {
}
};
thread.start();
与Http request
或socket
相关的任何代码或与网络相关的任何此类阻止UI执行的内容都会导致此异常