我正在尝试将twitter4j用于 Oauth 进程。我检查了安卓浏览器,打开谷歌后互联网连接仍然存在。
我运行以下代码片段。当我在我的活动中运行此代码时,我的模拟器页面中出现“不幸的是我的应用程序已关闭”。在 Logcat 中,我只看到:“ccc:try1”。我没有看到“trying2”。
可能这个错误不是因为 twitter4j,但我找不到错误。
Twitter twitter = TwitterFactory.getSingleton();
twitter.setOAuthConsumer("12345", "aaaa1212");
RequestToken requestToken = null;
try {
Log.d("ccc","trying1");
requestToken = twitter.getOAuthRequestToken();
Log.d("ccc","trying2");
} catch (TwitterException e) {
Log.d("unsuccess message:",e.getMessage());
Log.d("unsuccess statuscode:", Integer.toString(e.getStatusCode()));
Log.d("unsuccess errormessage:",e.getErrorMessage());
e.printStackTrace();
}
应用:
minSdkVersion="10"
targetSdkVersion="17"
模拟器版本 Android 4.2.2
还相关: 带有回调 URL 的 Twitter4j getOAuthRequestToken() 在 Android 上失败?
编辑:我很好奇非异步请求可能是问题所在。因为相同的代码适用于 Android 2.3.3。
编辑2:我得到android.os.NetworkOnMainThreadException
. 像这样的 Logcat 条目:
07-24 23:29:40.190 5455-5455/com.myexample.android D/ccc: trying1
07-24 23:29:40.520 5455-5455/com.myexample.android D/AndroidRuntime: Shutting down VM
07-24 23:29:40.535 5455-5455/com.myexample.android W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb4db7908)
07-24 23:29:40.660 5455-5455/com.myexample.android E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException:
Unable to start activity
ComponentInfo{com.myexample.android/com.myexample.android.MyActivity}:
android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:150)
at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:98)
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:122)
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:281)
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:273)
at com.myexample.android.MyActivity.onCreate(MyActivity.java:136)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more