关于http post的问题,发送多个变量

时间:2013-05-14 13:55:37

标签: android

我这里有这个代码,现在,我只能发送值参数,我无法发送其他参数。

我应该怎么做才能发送editText类型的其他值? 我希望能够发送:mbiemer使用httpost方法...但是如何...

由于

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_layout);
    value=(EditText)findViewById(R.id.editText1);
    mbiemer=(EditText)findViewById(R.id.msgMbiemer);
    telefon=(EditText)findViewById(R.id.msgTelefon);
    adresa=(EditText)findViewById(R.id.msgAdresa);
    ora=(EditText)findViewById(R.id.msgOra);
    per=(EditText)findViewById(R.id.msgPer);
    dyqan=(EditText)findViewById(R.id.msgDyqan);
    statusi=(EditText)findViewById(R.id.msgStatusi);

    btn=(Button)findViewById(R.id.button1);
    pb=(ProgressBar)findViewById(R.id.progressBar1);
    pb.setVisibility(View.GONE);
    btn.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    if(value.getText().toString().length() < 1) {
        // out of range
        Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
    } else {
        pb.setVisibility(View.VISIBLE);
        new MyAsyncTask().execute(value.getText().toString());  
    }
}

private class MyAsyncTask extends AsyncTask<String, Integer, Double> {

    @Override
    protected Double doInBackground(String... params) {
        // TODO Auto-generated method stub
        postData(params[0]);
        return null;
    }

    protected void onPostExecute(Double result){
    pb.setVisibility(View.GONE);
    Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
}

protected void onProgressUpdate(Integer... progress){
    pb.setProgress(progress[0]);
}

public void postData(String valueIWantToSend) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.10.28/app/app1.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

}
}

2 个答案:

答案 0 :(得分:2)

  

我应该怎么做才能发送editText的其他值   类型?

您可以使用ArrayList之类的任何数据结构来访问doInBackground中的值。第二个选项是,您可以使用VarargsAsyncTask.execute()中的序列中的所有参数传递为:

new MyAsyncTask().execute(value.getText().toString(),
             mbiemer.getText().toString(),
             telefon.getText().toString(),...); 

现在使用索引检索doInBackground中的所有值:

@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub

String str_value=params[0];
String str_mbiemer=params[1];
String str_telefon=params[2];
.....
postData(params[0]);
return null;
}

答案 1 :(得分:1)

您无法发送 mbiemer ,因为您没有发送

将您的代码更改为此

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.10.28/app/app1.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("myHttpData", value.getText().toString()));
    nameValuePairs.add(new BasicNameValuePair("mbiemer", mbiemer.getText().toString()));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

并调用postData();在你的doInBackground中