如何为在Android中点击的每个按钮分配不同的任务?

时间:2014-10-20 06:08:55

标签: android string button http-post

我想说我想点击一个按钮,然后从手机向服务器发送一个namevaluestring。 当我单击另一个按钮时,它会从应用程序向服务器发送另一个字符串数组。有办法吗?

Main.java

public class Main extends Activity implements OnClickListener{

private Button cnfrm;
private Button absnt;
private Button ntfy;
private ProgressBar pb;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cnfrm =(Button)findViewById(R.id.button1);
absnt =(Button)findViewById(R.id.button2);
ntfy =(Button)findViewById(R.id.button3);
pb=(ProgressBar)findViewById(R.id.progressBar2);
pb.setVisibility(View.GONE);
cnfrm.setOnClickListener(this);
absnt.setOnClickListener(this);
ntfy.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
    switch(v.getId())
    {
    case R.id.button1 :
        pb.setVisibility(View.VISIBLE);
        new MyAsyncTask().execute(toString());  
        break;
    case R.id.button2 :
        pb.setVisibility(View.VISIBLE);
        new MyAsyncTask().execute(toString());  
        break;
    case R.id.button3 :
        pb.setVisibility(View.VISIBLE);
        new MyAsyncTask().execute(toString());  
    break;

    }

}

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_SHORT).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://172.20.154.8:8080/Server/ChangeStatus");

try {
    // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("RestaurantID","0002"));
            nameValuePairs.add(new BasicNameValuePair("UserID","6585001342"));
            nameValuePairs.add(new BasicNameValuePair("checkin",Integer.toString(1)));
            nameValuePairs.add(new BasicNameValuePair("absent",Integer.toString(0)));
            nameValuePairs.add(new BasicNameValuePair("notify",Integer.toString(0)));
            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
    } }}}


    /*break;
case R.id.button2 :
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("RestaurantID","0002"));
        nameValuePairs.add(new BasicNameValuePair("UserID","6585001342"));
        nameValuePairs.add(new BasicNameValuePair("checkin",Integer.toString(0)));
        nameValuePairs.add(new BasicNameValuePair("absent",Integer.toString(1)));
        nameValuePairs.add(new BasicNameValuePair("notify",Integer.toString(0)));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost); }
    catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        } catch (IOException e) {
        // TODO Auto-generated catch block
        }
        break;
case R.id.button3 :
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("RestaurantID","0002"));
        nameValuePairs.add(new BasicNameValuePair("UserID","6585001342"));
        nameValuePairs.add(new BasicNameValuePair("checkin",Integer.toString(0)));
        nameValuePairs.add(new BasicNameValuePair("absent",Integer.toString(0)));
        nameValuePairs.add(new BasicNameValuePair("notify",Integer.toString(1)));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost); }
    catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        } catch (IOException e) {
        // TODO Auto-generated catch block
        }
        break;
    }
}*/

2 个答案:

答案 0 :(得分:1)

您可以像这样实现onClick事件。

public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1 :
    pb.setVisibility(View.VISIBLE);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("RestaurantID","0002"));
nameValuePairs.add(new BasicNameValuePair("UserID","6585001342"));
    new MyAsyncTask(nameValuePairs).execute(toString());  
    break;
case R.id.button2 :
    pb.setVisibility(View.VISIBLE);
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("RestaurantID","1111"));
nameValuePairs.add(new BasicNameValuePair("UserID","2222222"));
    new MyAsyncTask(nameValuePairs).execute(toString()); 
    break;
case R.id.button3 :
    pb.setVisibility(View.VISIBLE);
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("ABC","1"));
nameValuePairs.add(new BasicNameValuePair("PQR","2"));
    new MyAsyncTask(nameValuePairs).execute(toString());   
break;

}

}

这是您的AsyncTask实现

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
List<NameValuePair> nameValuePairs;
public MyAsyncTask(List<NameValuePair> nameValuePairs) {
    this.nameValuePairs=nameValuePairs;
}

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

protected void onPostExecute(Double result){
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "command sent",  
Toast.LENGTH_SHORT).show();
}
protected void onProgressUpdate(Integer... progress){
pb.setProgress(progress[0]);

}

 public void postData() {
// Create a new HttpClient and Post Header
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new      
 HttpPost("http://172.20.154.8:8080/Server/ChangeStatus");

 try {
// Add your data
          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
} }}}

我希望有了这个,你就可以得到基本的想法。祝你好运

答案 1 :(得分:0)

你可以尝试这样......

NameValuePair ..

定义公共变量
ArrayList<NameValuePair> params;

在班级中创建参数构造函数..

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

    List<NameValuePair> parameters = null;

    // parameter constructor..
    public MyAsyncTask (List<NameValuePair> params) {
        parameters = params;
    }



    @Override
    protected Double doInBackground(String... params) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(YOUR_WEB_ADDRESS);

    try {
        // this will pass different data everytime (which you have passed in costrocter.
        // make sure that you don't pass null value..
           if(parameters != null){
            httppost.setEntity(new UrlEncodedFormEntity(parameters));
            Log.v("MyAsyncTask", "Parameters :" + parameters); 
           }

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

    // DO YOUR CODING..

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

    return null;
    }
}

现在使用不同的参数为每个不同的按钮调用AsyncClass ..

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.button1 :
        pb.setVisibility(View.VISIBLE);
        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("KEY", VALUE));
        new MyAsyncTask(params).execute();  
        break;
    case R.id.button2 :
        pb.setVisibility(View.VISIBLE);
        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("KEY", VALUE));
        params.add(new BasicNameValuePair("KEY", VALUE));
        new MyAsyncTask(params).execute();  
        break;
    case R.id.button3 :
        pb.setVisibility(View.VISIBLE);
        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("KEY", VALUE));
        params.add(new BasicNameValuePair("KEY", VALUE));
        params.add(new BasicNameValuePair("KEY", VALUE));
        new MyAsyncTask(params).execute();  
    break;

    }

}

希望这会有所帮助......