【问题标题】:Scribe-java and async request in Android 4Android 4 中的 Scribe-java 和异步请求
【发布时间】:2013-12-19 17:50:56
【问题描述】:

在我的 Android 4.2.2 模拟器中,我尝试使用 scribe code sn-p:

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

在 logcat 中,我收到以下错误。我认为这可能是因为 Scribe 的这个示例代码不是异步任务。您能推荐一个异步示例吗?

请注意,此代码 sn-p 适用于 Android 2.3.3。它不适用于 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

【问题讨论】:

    标签: java scribe


    【解决方案1】:

    这是因为更高版本的 Android 现在要求网络连接发生在主线程之外。我以前为此使用过 AsyncTask,但在未来的 Android 版本中将弃用它。

    如果您需要接收结果并更新 UI,我建议您使用 AsyncTaskLoader。否则,您可以使用IntentService(或任何其他Service 类型)。您也可以直接创建一个新的Thread。无论您决定如何,一旦您将连接从主线程中移出,您将不会再收到NetworkOnMainThreadException

    【讨论】:

      【解决方案2】:

      在@Jabari 的回答之后,我也添加了我的回答。

      是的,网络连接的线程必须与 UI 线程不同。
      所以我发现loopj的库很好。
      你可以从这里得到:http://loopj.com/android-async-http/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-05
        • 2011-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多