0

我这里有这个代码,现在,我只能发送 value 参数,我不能发送其他的。

我应该怎么做才能发送 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
    }
}

}
}
4

2 回答 2

2

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

您可以使用任何数据结构,例如ArrayList访问doInBackground. AsyncTask.execute()第二个选项是您可以使用Varargs按顺序传递所有参数:

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;
}
于 2013-05-14T14:04:04.590 回答
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

于 2013-05-14T14:08:52.647 回答