Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用asynctask发送数据时强制关闭_Android - Fatal编程技术网

Android 使用asynctask发送数据时强制关闭

Android 使用asynctask发送数据时强制关闭,android,Android,我使用asynctask将http发送到服务器。我将传递的参数放入asynctask。这是我的任务 private class ProcessSend extends AsyncTask<String, Boolean, String> { //private HttpClient mHc = new DefaultHttpClient(); HttpClient httpclient = new DefaultHttpClient();

我使用asynctask将http发送到服务器。我将传递的参数放入asynctask。这是我的任务

private class ProcessSend extends AsyncTask<String, Boolean, String> {

        //private HttpClient mHc = new DefaultHttpClient();
        HttpClient httpclient = new DefaultHttpClient();
        private ProgressDialog pDialog;

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

            pDialog = new ProgressDialog(MainPetakTetap.this);
            pDialog.setMessage("Loading Send ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();

        }
        @Override
        protected String doInBackground(String... params) {
            publishProgress(true);
            // Do the usual httpclient thing to get the result
            String code = params[0];
            String alamat = params[1];
            String batas = params[2];
            String lat = params[3];
            String lon = params[4];
            String luas = params[5];
            String tglA = params[6];
            String tglB = params[7];
            String userId = params[8];
            //Toast.makeText(MainPetakTetap.this, "response : " + name, Toast.LENGTH_LONG).show();
            String url;
            url = CGlobalConfig.getURLRcvrPetakTetap();
            // Create a new HttpClient and Post Header

            HttpPost httppost = new HttpPost(url);

            //This is the data to send
            //String MyName = "puja"; //any data to send

            try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("code", code));
            nameValuePairs.add(new BasicNameValuePair("alamat", alamat));
            nameValuePairs.add(new BasicNameValuePair("info_batas", batas));
            nameValuePairs.add(new BasicNameValuePair("lat", lat));
            nameValuePairs.add(new BasicNameValuePair("lon", lon));
            nameValuePairs.add(new BasicNameValuePair("luas", luas));
            nameValuePairs.add(new BasicNameValuePair("tgl_awal", tglA));
            nameValuePairs.add(new BasicNameValuePair("tgl_akhir", tglB));
            nameValuePairs.add(new BasicNameValuePair("user_id", userId));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String response = httpclient.execute(httppost, responseHandler);

            //This is the response from a php application
            final String reverseString = response;
            runOnUiThread(new Runnable() {
                  public void run() {
                      Toast.makeText(MainPetakTetap.this, "response : " + reverseString, Toast.LENGTH_LONG).show();
                  }
                });

            //saveContact(code);
            } catch (final ClientProtocolException e) {
                runOnUiThread(new Runnable() {
                      public void run() {
                          Toast.makeText(MainPetakTetap.this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
                      }
                    });
            // TODO Auto-generated catch block
            } catch (final IOException e) {
                runOnUiThread(new Runnable() {
                      public void run() {
                          Toast.makeText(MainPetakTetap.this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
                      }
                    });
            // TODO Auto-generated catch block
            }

            // stop gps
            lman.removeUpdates(locaListener);
            constantsCursor.requery();
            return null;
        }

        @Override
        protected void onProgressUpdate(Boolean... progress) {
            // line below coupled with 
            //    getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS) 
            //    before setContentView 
            // will show the wait animation on the top-right corner
            MainPetakTetap.this.setProgressBarIndeterminateVisibility(progress[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            publishProgress(false);
            pDialog.dismiss();
            // Do something with result in your activity
        }

    }
如何解决这个问题


是否将运行ui线程从后台Do移动到onPostExecute?但是怎么做呢?

您需要将对cursor.requery的调用移动到onPostExecute。该游标绑定到列表适配器,因此它调用UI代码。您只能在主线程上执行此操作。

而不是
runOnUiThread()
,只需返回错误消息并在
onPostExecute()
中捕获它,然后您可以检查结果是否为
null
,如果不是,则显示
Toast
并执行其他操作。而且,最好是搬家

        lman.removeUpdates(locaListener);
        constantsCursor.requery();

onPostExecute()

您必须在onPostExecute中写入Toast消息。
只有创建视图层次结构的原始线程才能接触其视图。
应该会给您一个线索。
        lman.removeUpdates(locaListener);
        constantsCursor.requery();