1

我是为 android 创建应用程序的新手,也是第一次使用 asynctask。我想在后台运行一个 httpost,但不断出错。我使用正确的参数吗?我也需要后执行吗?

这是我的代码

public void send(View v) { new sendtask().execute(); }

   private class sendtask extends AsyncTask<String,Void, String> {


    String msg = msgTextField.getText().toString();  
    String msg1 = spinner1.getSelectedItem().toString();
    String msg2 = spinner2.getSelectedItem().toString();


    protected String doInBackground(String...url) {

          try {

               HttpClient httpclient = new DefaultHttpClient();
               HttpPost httppost = new HttpPost("http://10.0.2.2:80/test3.php");
               List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);;
               nameValuePairs.add(new BasicNameValuePair("id", "12345"));
               nameValuePairs.add(new BasicNameValuePair("name", msg));
               nameValuePairs.add(new BasicNameValuePair("gender",msg1));
               nameValuePairs.add(new BasicNameValuePair("age",msg2));
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));                   
               httpclient.execute(httppost);

               msgTextField.setText(""); // clear text box
             } catch (ClientProtocolException e) {
                 // TODO Auto-generated catch block
             } catch (IOException e) {
                 // TODO Auto-generated catch block
             }
        return null;
4

2 回答 2

1

OnPostExecute 用于需要更新用户界面(doInBackground(String...url)方法中不能更新用户界面),OnPostExecute接收的参数是doInBackground(String...url)返回的值,而不是您的案例是否与用户相关,如果发布了帖子,则通知您

于 2012-04-27T20:07:58.670 回答
0

您还应该包括错误以帮助识别和解决问题,但在这种情况下,解决方案可能如下:

private class sendtask extends AsyncTask<String, Void, String> {
    String msg = msgTextField.getText().toString();
    String msg1 = spinner1.getSelectedItem().toString();
    String msg2 = spinner2.getSelectedItem().toString();

    protected String doInBackground(String... url) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2:80/test3.php");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("name", msg));
            nameValuePairs.add(new BasicNameValuePair("gender", msg1));
            nameValuePairs.add(new BasicNameValuePair("age", msg2));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);

            return "";
        } catch (ClientProtocolException e) {
            return "ClientProtocolException";
        } catch (IOException e) {
            return "IOException";
        }
    }

    protected void onPostExecute(String result) {
        msgTextField.setText(result); // clear text box
    }
}

重要的变化是msgTextField.setText("");现在onPostExecute(),它接收要显示的文本doInBackground()。您必须在主线程上进行所有 UI 更改,也就是说,不能在doInBackground().

于 2012-04-27T20:43:35.893 回答