android.os.NetworkOnMainThreadException

时间:2013-07-29 07:45:39

标签: android exception android-asynctask networkonmainthread

我正在做门户网站应用程序。

使用AsyncTask<>即时获取json值并显示。

我的应用程序在Android 2.3.3中运行正常,但是当我在Android 4.1.2中运行该应用程序时,它会抛出错误。 我已经使用google并根据它进行了更改,但我仍然在4.1.2中收到错误

我已将该行列入清单..

<uses-permission android:name="android.permission.INTERNET" />

我也从onPostExecute(String file_url)删除了runOnUiThread部分,之后它也会抛出错误。

任何帮助都将不胜感激。

我的代码

class LoadAllgroups extends AsyncTask<String, String, String> {

         @Override
            protected void onPreExecute() {
                super.onPreExecute();

                showDialog(progress_bar_type);
            }

        protected String doInBackground(String... args) {
                 // here my codes

    }
                protected void onPostExecute(String file_url) {
            pDialog.dismiss();
            gnamelist=new ArrayList<String>(new LinkedHashSet<String>(gnamelist));
            runOnUiThread(new Runnable() {
                public void run() {
                    createExpandableListViewDialog();
                    }
            });
            return;
        }
        }

logcat的

07-29 00:29:47.860: E/AndroidRuntime(947): FATAL EXCEPTION: main
07-29 00:29:47.860: E/AndroidRuntime(947): android.os.NetworkOnMainThreadException
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-29 00:29:47.860: E/AndroidRuntime(947):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-29 00:29:47.860: E/AndroidRuntime(947):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-29 00:29:47.860: E/AndroidRuntime(947):  at libcore.io.IoBridge.connect(IoBridge.java:112)
07-29 00:29:47.860: E/AndroidRuntime(947):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-29 00:29:47.860: E/AndroidRuntime(947):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-29 00:29:47.860: E/AndroidRuntime(947):  at java.net.Socket.connect(Socket.java:842)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-29 00:29:47.860: E/AndroidRuntime(947):  at com.example.TEENEINSTIEN.JSONParser.makeHttpRequest(JSONParser.java:62)
07-29 00:29:47.860: E/AndroidRuntime(947):  at com.example.TEENEINSTIEN.FrontActivity$3.onGroupClick(FrontActivity.java:219)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:544)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.widget.AbsListView$1.run(AbsListView.java:3533)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.os.Handler.handleCallback(Handler.java:615)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.os.Looper.loop(Looper.java:137)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.app.ActivityThread.main(ActivityThread.java:4745)
07-29 00:29:47.860: E/AndroidRuntime(947):  at java.lang.reflect.Method.invokeNative(Native Method)
07-29 00:29:47.860: E/AndroidRuntime(947):  at java.lang.reflect.Method.invoke(Method.java:511)
07-29 00:29:47.860: E/AndroidRuntime(947):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-29 00:29:47.860: E/AndroidRuntime(947):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-29 00:29:47.860: E/AndroidRuntime(947):  at dalvik.system.NativeStart.main(Native Method)

Frontactivity.java行219中,JSONObject json = jsonParser.makeHttpRequest(url_get_groupid, "GET", params);方法为createExpandableListViewDialog()

1 个答案:

答案 0 :(得分:7)

您可以将以下代码放在oncreate中。但它不会解决它只会隐藏它的问题。

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

您实际需要做的是将createExpandableListViewDialog();(http请求部分)中的网络相关代码放在后台线程中。