0

我正在使用 Android facebook SDK 在 facebook 墙上发帖。我使用以下代码进行墙贴,但每次都出现异常android.os.NetworkOnMainThreadException

public void postToWall(String message) {
        Bundle parameters = new Bundle();
        parameters.putString("message", message);
        parameters.putString("description", "topic share");
        try {
            facebook.request("me");//Error here
            String response = facebook.request("me/feed", parameters, "POST");
            Log.d("Tests", "got response: " + response);
            if (response == null || response.equals("")
                    || response.equals("false")) {
                showToast("Blank response.");
            } else {
                showToast("Message posted to your facebook wall!");
            }
            finish();
        } catch (Exception e) {
            showToast("Failed to post to wall!");
            e.printStackTrace();
            finish();
        }
    }

日志猫

android.os.NetworkOnMainThreadException
W/System.err( 1048):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
W/System.err( 1048):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
W/System.err( 1048):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
W/System.err( 1048):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
W/System.err( 1048):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
W/System.err( 1048):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
W/System.err( 1048):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
W/System.err( 1048):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
W/System.err( 1048):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
W/System.err( 1048):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
W/System.err( 1048):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
W/System.err( 1048):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
W/System.err( 1048):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
W/System.err( 1048):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
W/System.err( 1048):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
W/System.err( 1048):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
W/System.err( 1048):    at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
W/System.err( 1048):    at com.facebook.android.Util.openUrl(Util.java:215)
W/System.err( 1048):    at com.facebook.android.Facebook.request(Facebook.java:776)
W/System.err( 1048):    at com.facebook.android.Facebook.request(Facebook.java:713)
W/System.err( 1048):    at com.example.share.Share.postToWall(Share.java:86)
W/System.err( 1048):    at com.example.share.Share.share(Share.java:72)
W/System.err( 1048):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 1048):    at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err( 1048):    at android.view.View$1.onClick(View.java:3586)
W/System.err( 1048):    at android.view.View.performClick(View.java:4084)
W/System.err( 1048):    at android.view.View$PerformClick.run(View.java:16966)
W/System.err( 1048):    at android.os.Handler.handleCallback(Handler.java:615)
W/System.err( 1048):    at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err( 1048):    at android.os.Looper.loop(Looper.java:137)
W/System.err( 1048):    at android.app.ActivityThread.main(ActivityThread.java:4745)
W/System.err( 1048):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 1048):    at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err( 1048):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
W/System.err( 1048):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
W/System.err( 1048):    at dalvik.system.NativeStart.main(Native Method)
W/WindowManager(  145): Failure taking screenshot for (246x410) to layer 21010
I/Choreographer( 1048): Skipped 40 frames!  The application may be doing too much work on its main thread.
D/dalvikvm(  226): GC_CONCURRENT freed 384K, 7% free 8535K/9095K, paused 20ms+4ms, total 52ms
4

1 回答 1

2

您不能在 Honeycombe 或更高版本的 UI 线程上执行网络 IO。从技术上讲,这在早期版本的 Android 上是可能的,但这是一个非常糟糕的主意,因为它会导致您的应用程序停止响应,并可能导致操作系统因您的应用程序行为不端而杀死您的应用程序。您需要运行后台进程或使用 AsyncTask 在后台线程上执行网络事务。

编辑:另一个想法,但它并不好。使用它是不好的做法,但有些人使用它。

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 

在您的活动类中添加上述代码。

我个人更喜欢第一个选项使用 thread 或 AsyncTask 。

于 2012-10-06T06:45:35.950 回答