Scribe-java and async request in Android 4

怎甘沉沦 提交于 2019-12-23 14:58:02

问题


In my Android 4.2.2 emulator I try to use scribe code snippet:

OAuthService service = new ServiceBuilder()
            .provider(TwitterApi.class)
            .apiKey("abc")
            .apiSecret("aaa123")
            .debug()
            .build();
    Scanner in = new Scanner(System.in);
    Token requestToken = service.getRequestToken();

In logcat I get the following error. I think it can be because of Scribe's this example code is not async-task. Can you recommend an async example please ?

Note that this code snippet works in Android 2.3.3. It doesn't work in Android 4.2.2.

    E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: 
      Unable to start activity  
      ComponentInfo{com.myexample.android/com.myexample.android.MyActivity}: 
      org.scribe.exceptions.OAuthConnectionException:
      There was a problem while creating a connection to the remote service.
    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: org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
    at org.scribe.model.Request.send(Request.java:70)
    at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:59)
    at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:40)
    at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:45)
    at com.myexample.android.MyActivity.onCreate(MyActivity.java:139)
    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
    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 org.scribe.model.Request.addBody(Request.java:136)
    at org.scribe.model.Request.doSend(Request.java:114)
    at org.scribe.model.Request.send(Request.java:66)
    ... 18 more

回答1:


This is because later versions of Android now require that network connections happen off of the main thread. I previously used AsyncTask for this, but it is being depracated in a future version of Android.

I suggest using AsyncTaskLoader if you need to receive a result back and update the UI. Otherwise, you can use an IntentService (or any other Service type). You can also create a new Thread directly. Whatever you decide, once you move the connection off of the main thread you won't receive a NetworkOnMainThreadException any more!




回答2:


After @Jabari's answer I add my answer also.

Yes, network connection must have different thread than UI thread.
So I found that loopj's library is very good.
You can get from this: http://loopj.com/android-async-http/



来源:https://stackoverflow.com/questions/17865468/scribe-java-and-async-request-in-android-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!