2013-07-25 51 views
4

異步請求在我的Android 4.2.2模擬器我嘗試用劃線的代碼片段:抄寫-Java和Android中4

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的這個例子代碼不是異步任務。你能推薦一個異步的例子嗎?

請注意,這段代碼片段適用於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 

回答

5

這是因爲Android的更高版本現在要求網絡連接發生在主線程之外。我以前使用過AsyncTask,但它在未來的Android版本中正在被討論。如果你需要接收結果並更新UI,我建議使用AsyncTaskLoader。否則,您可以使用IntentService(或任何其他Service類型)。您也可以直接創建新的​​。無論您決定如何,一旦將連接從主線移開,您將不會再收到NetworkOnMainThreadException

1

@ Jabari的答案後,我也加我的答案。

是的,網絡連接必須具有與UI線程不同的線程。
所以我發現loopj的庫非常好。
你可以從這裏得到:http://loopj.com/android-async-http/

+1

你能標記我的正確嗎? – Jabari