asynctask http帖子?

时间:2012-04-27 19:55:36

标签: android android-layout

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

这是我的代码

public void send(查看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;

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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()