-2

在第 215 行的 util 类上的 android facebook 代码中出现异常这里我打印堆栈:

04-05 11:08:34.015: E/AndroidRuntime(13527): FATAL EXCEPTION: main
04-05 11:08:34.015: E/AndroidRuntime(13527): android.os.NetworkOnMainThreadException
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.facebook.android.Util.openUrl(Util.java:215)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.facebook.android.Facebook.request(Facebook.java:755)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.facebook.android.Facebook.request(Facebook.java:692)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.agequiz.fbhelper.FacebookConnector$1.onAuthSucceed(FacebookConnector.java:184)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.agequiz.fbhelper.SessionEvents.onLoginSuccess(SessionEvents.java:78)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.agequiz.fbhelper.FacebookConnector$LoginDialogListener.onComplete(FacebookConnector.java:112)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.facebook.android.Facebook$1.onComplete(Facebook.java:358)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.facebook.android.FbDialog$FbWebViewClient.shouldOverrideUrlLoading(FbDialog.java:154)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:274)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:376)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.os.Looper.loop(Looper.java:137)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at android.app.ActivityThread.main(ActivityThread.java:4745)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at java.lang.reflect.Method.invokeNative(Native Method)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at java.lang.reflect.Method.invoke(Method.java:511)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-05 11:08:34.015: E/AndroidRuntime(13527):    at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

0

您当前的代码在uiThread上运行。考虑使用asynctask或扩展Thread的类。对此有很多问题,通过快速谷歌搜索,所以我觉得一个例子太多了。

于 2013-04-05T06:26:22.003 回答
0

当应用程序尝试在其主线程上执行网络操作时,将引发此异常。在 AsyncTask 中运行您的代码:

private class Retreive extends AsyncTask<Void, Void, Void> {

protected Void doInBackground(Void... urls) {       
    return null;         
}
于 2013-04-05T06:26:56.787 回答
-2

当应用程序尝试在其主线程上执行网络操作时,将引发此异常。AsyncTask在或 中运行您的代码Thread

onCreate()或在方法中添加以下代码。

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
于 2013-04-05T06:28:49.433 回答