我想了解asynctask,假设我有5-6服务器api,现在我想从每个api获得响应,但条件是我只想使用单个asynctask。
现在只有一个Asynctask,但是执行没有正确完成。
private class HttpAsynctaskinvitatioal extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... urls) {
return POST(urls[0]);
}
protected void onPostExecute(String result)
{
super.onPostExecute(result);
invitatioalurl=result.trim();
}
}发布于 2015-07-17 09:52:55
您可以如下所示创建AsyncClass:
public class BackgroundNetwork extends AsyncTask<String, String, String> {
Context context;
ProgressDialog progress;
@Override
protected void onPreExecute() {
progress = new ProgressDialog(context);
progress.setTitle("Loading...");
progress.show();
super.onPreExecute();
}
public BackgroundNetwork(Context activity) {
context = activity;
}
@Override
protected void onPostExecute(String result) {
if (progress.isShowing()) {
progress.dismiss();
}
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void... params) {
return null;
}
}然后,您可以按如下方式调用这个类:
new BackgroundNetwork(Activity.this){
protected Void doInBackground(Void... params) {
String result = params[0];
//call api here or any background work you want to execute
return result;
}
protected void onPostExecute(String result) {
super.onPostExecute(result); //this is a important line to add.
//get your response result here then do UI changes whatever you want to do.
}
}.execute();发布于 2015-07-17 09:42:42
这将从您的技术逻辑。只需调用单个异步任务,但以不同的方式获得每个url的响应。
发布于 2015-07-17 09:48:25
传递不同的urls作为参数。
或
在构造函数中设置它。
https://stackoverflow.com/questions/31472849
复制相似问题