facebook壁纸上的android.os.NetworkOnMainThreadException

时间:2012-10-06 06:41:50

标签: android facebook facebook-wall networkonmainthread

我正在使用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();
        }
    }

logcat的

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

1 个答案:

答案 0 :(得分:2)

  

您无法在Honeycombe或更高版本的UI线程上执行网络IO。   从技术上讲,它可以在早期版本的Android上使用,但它是一个   真的很糟糕,因为它会导致你的应用停止响应,并且可以   导致操作系统因为表现不佳而导致您的应用被杀。你需要   运行后台进程或使用AsyncTask来执行您的网络   在后台线程上进行交易。

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

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

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

我个人首选使用线程或AsyncTask。