【发布时间】:2012-04-08 06:23:22
【问题描述】:
我有一个扩展 AsyncTask 的类。
在 doInBackground() 方法中,我连接到服务器并获取数据。
该类有一个方法 get() 可以将此数据返回给用户。
所以我在这个类中调用 execute() 方法,不久之后,返回一个成员变量“response”。
但由于尚未建立连接,因此返回 null。
所以,我在 onPostExecute() 中设置了变量“response”,并希望从这里返回到主线程。
如果不使用这样的循环,我将如何做到这一点:
while(! gotResponse) continue;
我的伪代码:
//Main thread
class blah {
.... onCreate(..) {
ConHelper ch = new ConHelper();
String data = ch.get();
}
}
class ConHelper extends AsyncTask<...>{
private String response;
private boolean gotResponse;
... get() {
this.execute(...);
while(! this.gotResponse) continue;
return this.response;
}
... doInBackground(..) {
// connect and get data.
}
... on postExecute(...) {
//set response and gotResponse from obtained data.
}
【问题讨论】:
标签: android multithreading android-asynctask httpconnection