问题
I need to call asynctask with different urls. i have three button, they do a http post to server with different urls, now i can call asynctask with one url but how can i call the same function with different urls.the following is my asynctask class:
private class Downloadfiles extends AsyncTask<URL, Integer, JSONObject>{
@Override
protected void onPostExecute(JSONObject jo) {
try {
cards = jo.getInt("cards");
points = jo.getInt("points");
cash = jo.getInt("cash");
} catch (JSONException e1) {
e1.printStackTrace();
}
//cardsv.startAnimation(textio);
cardsv.setText(""+cards);
cashv.setText(""+cash);
Log.e("wintype", "msg" + wintype);
super.onPostExecute(jo);
}
@Override
protected JSONObject doInBackground(URL... params) {
Log.e("msg++--", "play game method called");
BufferedReader reader=null;
data_to_send = "userId=" + userId ;
try
{
Log.e("inside try block playgame", "get text" + data_to_send);
// Defined URL where to send data
URL url = new URL(playgame);
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data_to_send);
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
Log.e("inside playgame", "while loop");
}
play_response = sb.toString();
}
catch(Exception ex)
{
Log.e("MyTag ", "Failure to get json data in playgame--1--", ex);
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {
Log.e("MyTag", "Failure to get json data in playgame--2--", ex);
}
}
Log.e("play response from the server in ", "play game" + play_response);
JSONObject jo = null;
try {
jo = new JSONObject(play_response);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jo;
}
}
and i am calling this with one of my buttons click
Downloadfiles download = new Downloadfiles();
downloads.execute();
回答1:
Change
private class Downloadfiles extends AsyncTask<URL, Integer, JSONObject>{
to
private class Downloadfiles extends AsyncTask<String, Integer, JSONObject>{
And change
@Override
protected JSONObject doInBackground(URL... params) {
Log.e("msg++--", "play game method called");
to
@Override
protected JSONObject doInBackground(String... urls) {
String url= urls[0];// get the URL String here
And Finally call the AsyncTask
like
new Downloadfiles (this).execute(URL1);// For Button1
and
new Downloadfiles (this).execute(URL2);//For Button2 and so on
回答2:
Pass the url as a parameter in the constructor of async task.
回答3:
AsyncTask < Params , Progress, Result >
In your case you declared an AsyncTask< URL, Integer , JSONObject > it mean you ll provide
- Params of type : URL
- Progress : Integer
- Result : JSONObject
the Method doInBackground(URL... params) accept multiplme parameters, (URL... params) is equivalent to (URL[] params), and you can use it like :
URL[] urls= ....."many url here" ;
Downloadfiles download = new Downloadfiles(urls);
downloads.execute();
inside doInBackground Loop over urls to do job
回答4:
Pass the url as a parameter in the constructor of async task.
change
private class Downloadfiles extends AsyncTask<URL, Integer, JSONObject>
to
private class Downloadfiles extends AsyncTask<String, Integer, JSONObject>
And change
protected JSONObject doInBackground(URL... params)
to
protected JSONObject doInBackground(String... params)
private class Downloadfiles extends AsyncTask<String, Integer, JSONObject>{
String URL;
Context _context;
public Downloadfiles (Context context, String URL)
{
this.URL = URL;
_context = context;
}
}
Call it from activity like this
Button 1
new Downloadfiles (MainActivity.this, "URL1").execute();
Button 2
new Downloadfiles (MainActivity.this, "URL2").execute();
来源:https://stackoverflow.com/questions/23538748/how-to-call-async-task-with-different-url