orkOnMainThreadException在Faceboo

orkOnMainThreadException在Faceboo

本文介绍了android.os.NetworkOnMainThreadException在Facebook的墙后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Android的Facebook SDK进行后期在Facebook wall.I已经使用下面的$ C $下的墙后,但通过例外每次 android.os.NetworkOnMainThreadException

 公共无效postToWall(字符串消息){
        捆绑参数=新包();
        parameters.putString(消息,消息);
        parameters.putString(说明,主题股);
        尝试 {
            facebook.request(我); //这里出错
            字符串响应= facebook.request(ME /饲料,参数,POST);
            Log.d(测试,得到了回应:+响应);
            如果(响应==空|| response.equals()
                    || response.equals(假)){
                showToast(空白回应。);
            } 其他 {
                showToast(消息发布到你的Facebook墙上!);
            }
            完();
        }赶上(例外五){
            showToast(无法​​张贴到墙上!);
            e.printStackTrace();
            完();
        }
    }
 

的logcat

  android.os.NetworkOnMainThreadException
W / System.err的(1048):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
W / System.err的(1048):在java.net.InetAddress.lookupHostByName(InetAddress.java:385)
W / System.err的(1048):在java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
W / System.err的(1048):在java.net.InetAddress.getAllByName(InetAddress.java:214)
W / System.err的(1048):在libcore.net.http.HttpConnection&其中;初始化>(HttpConnection.java:70)
W / System.err的(1048):在libcore.net.http.HttpConnection&其中;初始化>(HttpConnection.java:50)
W / System.err的(1048):在libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:341)
W / System.err的(1048):在libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
W / System.err的(1048):在libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
W / System.err的(1048):在libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
W / System.err的(1048):在libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
W / System.err的(1048):在libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
W / System.err的(1048):在libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
W / System.err的(1048):在libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
W / System.err的(1048):在libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
W / System.err的(1048):在libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
W / System.err的(1048):在libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
W / System.err的(1048):在com.facebook.android.Util.openUrl(Util.java:215)
W / System.err的(1048):在com.facebook.android.Facebook.request(Facebook.java:776)
W / System.err的(1048):在com.facebook.android.Facebook.request(Facebook.java:713)
W / System.err的(1048):在com.example.share.Share.postToWall(Share.java:86)
W / System.err的(1048):在com.example.share.Share.share(Share.java:72)
W / System.err的(1048):在java.lang.reflect.Method.invokeNative(本机方法)
W / System.err的(1048):在java.lang.reflect.Method.invoke(Method.java:511)
W / System.err的(1048):在android.view.View $ 1.onClick(View.java:3586)
W / System.err的(1048):在android.view.View.performClick(View.java:4084)
W / System.err的(1048):在android.view.View $ PerformClick.run(View.java:16966)
W / System.err的(1048):在android.os.Handler.handleCallback(Handler.java:615)
W / System.err的(1048):在android.os.Handler.dispatchMessage(Handler.java:92)
W / System.err的(1048):在android.os.Looper.loop(Looper.java:137)
W / System.err的(1048):在android.app.ActivityThread.main(ActivityThread.java:4745)
W / System.err的(1048):在java.lang.reflect.Method.invokeNative(本机方法)
W / System.err的(1048):在java.lang.reflect.Method.invoke(Method.java:511)
W / System.err的(1048):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
W / System.err的(1048):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
W / System.err的(1048):在dalvik.system.NativeStart.main(本机方法)
W /窗口管理器(145):未能采取截图(246x410)到层21010
I /编舞(1048):跳过40帧!该应用程序可能会做它的主线程的工作太多了。
D / dalvikvm(226):GC_CONCURRENT释放384K,7%免费8535K / 9095K,暂停20毫秒+ 4ms的,总52ms
 

解决方案

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

  StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建立()。;
StrictMode.setThreadPolicy(政策);
 

上面添加code。在您的活动类。

我个人preFER第一个选项使用线程或AsyncTask的。

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
解决方案

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 .

这篇关于android.os.NetworkOnMainThreadException在Facebook的墙后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:49