4

I'm designing an application that frequently communicates to a web server for updates. Communication happens only when user requests. I found AsyncTask could be helpful here. So I modified one class to serve my application as AsyncTask.

I want to pass an url and http post parameters to anysc class's doInBackground process. I can't figure out how to do it.

Here's it-

public class GetXMLFromServer extends
    AsyncTask< String, Void, String> {
private Context context;

GetXMLCallback gc = null;

ProgressDialog progressDialog;

public GetXMLFromServer(Context context, GetXMLCallback gc) {
    this.context = context;
    this.gc = gc;
    progressDialog = new ProgressDialog(this.context);
}

protected void onPreExecute() {
    progressDialog.setMessage("Fetching...");
    progressDialog.show();
}

@Override
protected void onPostExecute(String result) {
    gc.onSuccess(result);
    progressDialog.dismiss();
}


 @Override
protected String doInBackground(String... params) {
     String response = "";
    response=CustomHttpClient.executeHttpPost(params[0]);
    return null;
} 
    //Confused how to pass URL and http post parameters to doInBackground().
  }

I've one interface that is used to process response sent from onPostExecute(). It is as fallows.

   package com.project.main.external;

   public interface GetXMLCallback {
       void onSuccess(String downloadedString);
       void onFailure(Exception exception);
   }

And here is my main activity that calls for http response --

 public class UpdateList extends Activity implements GetXMLCallback { 
 //above line also throws error that interface methods are not implemented yet
 //they are (few lines below) defined.

private TextView textView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout);
    textView = (TextView) findViewById(R.id.TextView01);
}

GetXMLCallback gc = new GetXMLCallback() {

    public void onFailure(Exception exception) {

    }

    public void onSuccess(String downloadedString) {
        textView.setText(downloadedString);
    }

};

public void getUpdates(View view) {
    GetXMLFromServer task = new GetXMLFromServer(UpdateList.this, gc);
    task.execute(WebConstants.GET_UPDATES);
}
    }
SachinGutte
  • 6,947
  • 5
  • 35
  • 58
  • possible duplicate of [AsyncTask's doInBackground(Params... params)](http://stackoverflow.com/questions/6343064/asynctasks-doinbackgroundparams-params) – Aaron McIver Feb 16 '12 at 15:29

3 Answers3

10

AsyncTask does already allow more parameters:
In your case call it like this:

task.execute("parameter_one","parameter_two","parameter_three");

In doInBackground you can get them with:

params[0]
params[1]
params[2]

And so on

Just notice that all parameters must be of the same Type, in your case String.

Thommy
  • 5,070
  • 2
  • 28
  • 51
4
  1. You could add one or more things to the constructor that you already have and populate some members of the AsyncTask and then use those members to make your POST within doInBackground
  2. You could change it to extend AsyncTask<HttpPost, Void, String> or AsyncTask<SOME_CLASS, Void, String> where SOME_CLASS is an ArrayList of Strings, a HashMap, or some other class that you could create that would contain everything you need to construct the HttpPost
Sam Dozor
  • 40,335
  • 6
  • 42
  • 42
0
public class GetXMLFromServer extends
AsyncTask< String, Void, String> {
private Context context;

GetXMLCallback gc = null;

ProgressDialog progressDialog;

public Type variable1;
public Type variable2;

and set up the variables:

GetXMLFromServer task = new GetXMLFromServer(UpdateList.this, gc);
task.variable1 = XXXXX;
task.variable2 = XXXXX;
task.execute(WebConstants.GET_UPDATES);
goodm
  • 7,275
  • 6
  • 31
  • 55
  • thanks it worked. But I still can't solve error saying " unimplemented method " from GerXMLCallback interface. How can i solve it ? – SachinGutte Feb 16 '12 at 15:42
  • I think this answers the question "Passing two parametes to asynctask method. how to?" – Daniel Feb 16 '12 at 15:59
  • this is not an all-exclusive answer. why do this when `asynctask ` `doInBackground` method accepts as many arguments as you want. pay attention to `doInBackground(String... params)` . this means you can pass in as many as String you want and access it in an array called params :) – hadi Jul 06 '15 at 15:52