android.os.NetworkOnMainThreadException in facebook wall post

爱⌒轻易说出口 提交于 2019-12-12 00:32:54

问题


I'm using Android facebook SDK for post on facebook wall.I have used following code for wall post but every time through exception 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:


You cannot perform network IO on the UI thread on Honeycombe or later. Technically it is possible on earlier versions of Android, but is a really bad idea as it will cause your app to stop responding, and can result in the OS killing your app for being badly behaved. You'll need to run a background process or use AsyncTask to perform your network transaction on a background thread.

Edit : Another Idea but it is not good . It is Bad Practice to use it but some people use it.

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

Add above code in your activity class.

I personally Prefer the first option use thread or AsyncTask .



来源:https://stackoverflow.com/questions/12757416/android-os-networkonmainthreadexception-in-facebook-wall-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!