我有一个run()
方法,在其中我正在执行一个正在执行 HTTPGET 的 Asynctask。如何run()
等待 Asynctask 获得结果然后继续?
问问题
90 次
2 回答
0
//like this
asyncRequest(URL, parameters, "POST",new RequestListener() {
@Override
public void onComplete(String response) {
/*@todo get results and then continue */
}
@Override
public void onIOException(IOException ex) {
}
@Override
public void onFileNotFoundException(FileNotFoundException ex) {
}
@Override
public void onMalformedURLException(MalformedURLException ex) {
}
});
public static void asyncRequest(final String requestUrl,final Bundle parameters,final String httpMethod,final RequestListener requestListener) {
new Thread() {
public void run() {
try {
String resp = MyUtil.openUrl(requestUrl, httpMethod, parameters);
requestListener.onComplete(resp);
} catch (FileNotFoundException ex) {
requestListener.onFileNotFoundException(ex);
} catch (MalformedURLException ex) {
requestListener.onMalformedURLException(ex);
} catch (IOException ex) {
requestListener.onIOException(ex);
}
}
}.start();
}
public static interface RequestListener {
public void onComplete(String response);
public void onIOException(IOException ex);
public void onFileNotFoundException(FileNotFoundException ex);
public void onMalformedURLException(MalformedURLException ex);
}
于 2012-11-14T07:59:10.953 回答
-1
在AsyncTask的onPostExecute方法中执行run函数
private class LongOperation extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
//do long asynctask activity here
}
@Override
protected void onPostExecute(String result) {
run(); //executes the run method you want here after asysnctask is completed
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
于 2012-11-14T12:41:41.073 回答