由於您的服務器調用以異步方式已經執行,你不需要調用它doInBackground()
,只需使用您的服務器通信解決方案提供的設施即可。
如果你想更新結果數據的UI元素,並獲得must be called from main thread
,則需要手動發表您的結果處理邏輯與UI AKA主要線程這樣的:
ServerLoader.getSharedInstance().get(url,
new new AsyncCallback<GetResult<Long>>() {
@Override
public void onFailure(Exception e) {
}
@Override
public void onSuccess(GetResult<Long> result) {
final Long myResult = result.getValue();
// .post() on a view is what sends stuff to UI thread
txtView.post(new Runnable(){
public void run(){
txtView.setText("" + myResult);
}
});
}
);
如果你絕對設置與AsyncTask
進行代理您的通話ServerLoader
,你可以實現一個骯髒的墊片這樣的:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected CountDownLatch mLatch = null;
protected Long mResult = null;
protected Long doInBackground(URL... urls) {
if (urls == null || urls.length = 0) {
return null;
}
mLatch = new CountDownLatch(1);
ServerLoader.getSharedInstance().get(url[0],
new new AsyncCallback<GetResult<Long>>() {
@Override
public void onFailure(Exception e) {
// leave the result null
mLatch.countDown();
}
@Override
public void onSuccess(GetResult<Long> result) {
mResult = result.getValue();
mLatch.countDown();
}
);
// the thread that DownloadFilesTask.doInBackground()
// gets executed on will wait till it hears from ServerLoader
mLatch.await();
return mResult;
}
protected void onPostExecute(Long result) {
// here you get the result
}
}
不要我們e asynctask,那麼,你的電話已經在後臺線程中播放了。 – njzk2
我需要它在後臺線程上完成並在調用後更新UI。 – confile
是的。這就是你的回調。 – njzk2