-2

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

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

我的应用程序在 Android 2.3.3 中运行良好,但是当我在 android 4.1.2 中运行该应用程序时,它会抛出错误。我已经使用谷歌并根据它进行了更改,但我仍然在 4.1.2 中遇到错误

我也在清单中包含了这条线..

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

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

任何帮助,将不胜感激。

我的代码

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;
        }
        }

日志猫

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()

4

1 回答 1

7

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

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

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

于 2013-07-29T09:10:47.820 回答