我有一个应用程序可以进行大量 RESTful 服务调用。我在扩展 Asynctask 的类中执行调用。如果我必须取消异步任务,我也想取消服务调用。不幸的是,取消异步操作仍然允许 doInBackground 完成,并且一旦请求正在等待响应(这可能需要一点时间),我就无法调用 isCancelled() 。现在,在我的 doInBackground 方法中,我正在注册以在发出取消请求时从 UI 线程收到通知,因此我可以中止 HttpResponse 对象。这是一段示例代码。
到目前为止它一直有效,但我真的可以指望它,还是我只是走运?你能指望一个线程调用另一个线程中的方法吗?
public class AsyncTestActivity extends Activity {
private ArrayList<IStopRequestMonitor> monitors;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
public void stopActivity() {
if (monitors == null || monitors.size() < 1) return;
for (int i = 0; i < monitors.size(); i++) {
monitors.get(i).stopRequest();
}
}
public void addListener(IStopRequestMonitor listener) {
if (monitors == null) monitors = new ArrayList<IStopRequestMonitor>();
monitors.add(listener);
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.mywebsite.com/feeds/rsstest.xml" });
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
DefaultHttpClient client = new DefaultHttpClient();
final HttpGet httpGet = new HttpGet(urls[0]);
addListener(new IStopRequestMonitor() {
public void stopRequest() {
if (httpGet == null) return;
httpGet.abort();
cancel(true);
}
});
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
// handle inputstream
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String result) {
Log.d("Result:", result);
}
}
interface IStopRequestMonitor {
public void stopRequest();
}
}