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

问题描述

 类GetDetails扩展的AsyncTask<字符串,字符串,字符串> {

            @覆盖
            在preExecute保护无效(){
                super.on preExecute();
                pDialog =新ProgressDialog(EditProduct.this);
                pDialog.setMessage(Chargement Patienter ......。);
                pDialog.setIndeterminate(假);
                pDialog.setCancelable(真正的);
                pDialog.show();
            }


            保护字符串doInBackground(字符串... PARAMS){


                runOnUiThread(新的Runnable(){
                    公共无效的run(){

                        诠释成功;
                        尝试 {

                            名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
                            params.add(新BasicNameValuePair(PID,PID));


                            JSONObject的JSON = jsonParser.makeHtt prequest(
                                    url_product_detials,GET,则params);


                            Log.d(单一产品详细信息,json.toString());


                            成功= json.getInt(TAG_SUCCESS);
                            如果(成功== 1){

                                JSONArray productObj = json.getJSONArray(TAG_PRODUCT);


                                的JSONObject产物= productObj.getJSONObject(0);


                                txtName的=(的EditText)findViewById(R.id.inputName);
                                txtLongi =(EditText上)findViewById(R.id.inputLongi);
                                txtLati =(EditText上)findViewById(R.id.inputLati);
                                txtRegion =(EditText上)findViewById(R.id.inputRegion);


                                txtName.setText(product.getString(TAG_NAME));
                                txtLongi.setText(product.getString(TAG_PRICE));
                                txtLati.setText(product.getString(TAG_DESCRIPTION));
                                txtRegion.setText(product.getString(TAG_LOCATION));

                            }其他{

                            }
                        }赶上(JSONException E){
                            e.printStackTrace();
                        }
                    }
                });

                返回null;
            }



            保护无效onPostExecute(字符串file_url){

                pDialog.dismiss();
            }
        }
 

LogCat中:

  11-12 22:42:12.552:E / AndroidRuntime(1100):致命异常:主要
11-12 22:42:12.552:E / AndroidRuntime(1100):android.os.NetworkOnMainThreadException
11-12 22:42:12.552:E / AndroidRuntime(1100):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-12 22:42:12.552:E / AndroidRuntime(1100):在libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
11-12 22:42:12.552:E / AndroidRuntime(1100):在libcore.io.IoBridge.connectErrno(IoBridge.java:127)
11-12 22:42:12.552:E / AndroidRuntime(1100):在libcore.io.IoBridge.connect(IoBridge.java:112)
11-12 22:42:12.552:E / AndroidRuntime(1100):在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-12 22:42:12.552:E / AndroidRuntime(1100):在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-12 22:42:12.552:E / AndroidRuntime(1100):在java.net.Socket.connect(Socket.java:842)
11-12 22:42:12.552:E / AndroidRuntime(1100):在org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
11-12 22:42:12.552:E / AndroidRuntime(1100):在org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
11-12 22:42:12.552:E / AndroidRuntime(1100):在org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-12 22:42:12.552:E / AndroidRuntime(1100):在org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-12 22:42:12.552:E / AndroidRuntime(1100):在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-12 22:42:12.552:E / AndroidRuntime(1100):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-12 22:42:12.552:E / AndroidRuntime(1100):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-12 22:42:12.552:E / AndroidRuntime(1100):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-12 22:42:12.552:E / AndroidRuntime(1100):在com.example.androidhive.JSONParser.makeHtt prequest(JSONParser.java:62)
11-12 22:42:12.552:E / AndroidRuntime(1100):在fitness.coter.serveur.EditProduct $ GetPharmacieDetails $ 1.运行(EditProduct.java:135)
11-12 22:42:12.552:E / AndroidRuntime(1100):在android.os.Handler.handleCallback(Handler.java:725)
11-12 22:42:12.552:E / AndroidRuntime(1100):在android.os.Handler.dispatchMessage(Handler.java:92)
11-12 22:42:12.552:E / AndroidRuntime(1100):在android.os.Looper.loop(Looper.java:137)
11-12 22:42:12.552:E / AndroidRuntime(1100):在android.app.ActivityThread.main(ActivityThread.java:5041)
11-12 22:42:12.552:E / AndroidRuntime(1100):在java.lang.reflect.Method.invokeNative(本机方法)
11-12 22:42:12.552:E / AndroidRuntime(1100):在java.lang.reflect.Method.invoke(Method.java:511)
11-12 22:42:12.552:E / AndroidRuntime(1100):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
11-12 22:42:12.552:E / AndroidRuntime(1100):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-12 22:42:12.552:E / AndroidRuntime(1100):在dalvik.system.NativeStart.main(本机方法)
11-12 22:42:18.021:I /过程(1100):发送信号。 PID:1100 SIG:9
11-12 22:42:20.212:E /跟踪(1130):错误打开跟踪文件:没有这样的文件或目录(2)
 

解决方案

删除你的 runOnUiThread() code doInBackground(),这不是多么的AsyncTask 是为了工作

  runOnUiThread(新的Runnable(){
                公共无效的run(){
 

在每一个方法的的AsyncTask 除了 doInBackground()运行在 UI线程。因此,做好 doInBackground网络的东西(),并根据需要使用其他方法。

onProgressUpdate()可用于更新 UI 执行时doInBackground( )

onPostExecute()可用于接收来自结果doInBackground()和更新 UI 相应

在preExecute()可用于更新 UI doInBackground()运行的东西,如呈现出 ProgressDialog

请仔细阅读AsyncTask的文档几次。这是一开始有点棘手,但一旦你理解如何将其意味着工作,那么它可以是一个伟大的事情。

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

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(EditProduct.this);
                pDialog.setMessage("Chargement. Patienter ...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }


            protected String doInBackground(String... params) {


                runOnUiThread(new Runnable() {
                    public void run() {

                        int success;
                        try {

                            List<NameValuePair> params = new ArrayList<NameValuePair>();
                            params.add(new BasicNameValuePair("pid", pid));


                            JSONObject json = jsonParser.makeHttpRequest(
                                    url_product_detials, "GET", params);


                            Log.d("Single Product Details", json.toString());


                            success = json.getInt(TAG_SUCCESS);
                            if (success == 1) {

                                JSONArray productObj = json.getJSONArray(TAG_PRODUCT); 


                                JSONObject product = productObj.getJSONObject(0);


                                txtName = (EditText) findViewById(R.id.inputName);
                                txtLongi = (EditText) findViewById(R.id.inputLongi);
                                txtLati = (EditText) findViewById(R.id.inputLati);
                                txtRegion = (EditText) findViewById(R.id.inputRegion);


                                txtName.setText(product.getString(TAG_NAME));
                                txtLongi.setText(product.getString(TAG_PRICE));
                                txtLati.setText(product.getString(TAG_DESCRIPTION));
                                txtRegion.setText(product.getString(TAG_LOCATION));

                            }else{

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

                return null;
            }



            protected void onPostExecute(String file_url) {

                pDialog.dismiss();
            }
        }

LogCat:

11-12 22:42:12.552: E/AndroidRuntime(1100): FATAL EXCEPTION: main
11-12 22:42:12.552: E/AndroidRuntime(1100): android.os.NetworkOnMainThreadException
11-12 22:42:12.552: E/AndroidRuntime(1100):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at libcore.io.IoBridge.connect(IoBridge.java:112)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at java.net.Socket.connect(Socket.java:842)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at com.example.androidhive.JSONParser.makeHttpRequest(JSONParser.java:62)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at fitness.coter.serveur.EditProduct$GetPharmacieDetails$1.run(EditProduct.java:135)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at android.os.Handler.handleCallback(Handler.java:725)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at android.os.Looper.loop(Looper.java:137)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at java.lang.reflect.Method.invoke(Method.java:511)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-12 22:42:12.552: E/AndroidRuntime(1100):     at dalvik.system.NativeStart.main(Native Method)
11-12 22:42:18.021: I/Process(1100): Sending signal. PID: 1100 SIG: 9
11-12 22:42:20.212: E/Trace(1130): error opening trace file: No such file or directory (2)
解决方案

Remove your runOnUiThread() code from doInBackground(), that's not how AsyncTask is meant to work

runOnUiThread(new Runnable() {
                public void run() {

Every method in an AsyncTask except for doInBackground() runs on the UI Thread. Therefore, do your network stuff in doInBackground() and use the other methods as needed.

onProgressUpdate() can be used to update the UI during execution of doInBackground()

onPostExecute() can be used to receive a result from doInBackground() and update the UI accordingly

onPreExecute() can be used to update the UI before doInBackground() runs for things such as showing a ProgressDialog

Please read through the AsyncTask Docs several times. It is a little tricky at the beginning but once you understand how its meant to work then it can be a great thing.

这篇关于android.os.NetworkOnMainThreadException在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 09:59