我是为 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;