android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)中的android.os.NetworkOnMainThreadException

时间:2013-11-12 22:58:12

标签: android

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)

3 个答案:

答案 0 :(得分:6)

runOnUiThread()移除doInBackground()代码,而不是AsyncTask的工作方式

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

AsyncTask中除doInBackground()外的每个方法都在UI Thread上运行。因此,请在doInBackground()中填写您的网络内容,并根据需要使用其他方法。

onProgressUpdate()可用于在执行UI期间更新doInBackground()

onPostExecute()可用于接收doInBackground()的结果并相应更新UI

onPreExecute()可用于在UI运行之前更新doInBackground(),例如显示ProgressDialog

Please read through the AsyncTask Docs几次。这一开始有点棘手,但是一旦你理解了它的工作意义,它就会很棒。

答案 1 :(得分:1)

您的后台任务正在通过调用runOnUiThread来执行其逻辑。不要那样做。只需在doInBackground

中直接执行网络I / O.

在这种情况下调用runonUiThread会失败使用AsyncTask的全部目的。

答案 2 :(得分:0)

“makeHttpRequest(url_product_detials,”GET“,params)”

就像异常所说:在mainthread上没有(!)网络操作。