-6
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();
            }
        }

日志猫:

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

3 回答 3

6

从中删除您的runOnUiThread()代码doInBackground(),这不是AsyncTask工作方式

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

中的每个方法AsyncTaskdoInBackground()运行在UI Thread. 因此,请doInBackground()根据需要进行网络处理并使用其他方法。

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

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

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

请仔细阅读 AsyncTask Docs几次。一开始有点棘手,但是一旦您了解了它的工作原理,那将是一件很棒的事情。

于 2013-11-12T23:01:18.923 回答
1

您的后台任务正在通过调用runOnUiThread. 不要那样做。只需直接在doInBackground.

在这种情况下调用runonUiThread会破坏使用AsyncTask.

于 2013-11-12T23:01:39.797 回答
0

"makeHttpRequest(url_product_detials, "GET", 参数)"

就像例外说的那样:主线程上没有(!)网络操作。

于 2013-11-12T23:01:00.203 回答