【发布时间】:2016-10-10 04:03:15
【问题描述】:
private class exampleTask extends AsyncTask<String, Void, SomeResult>{
@Override
protected SomeResult doInBackground(String... urls) {
SomeResult res ;
someMethod(new CallBack<T>(){
@Override
public void onResponse(SomeResult something) {
res = something ;
}
@Override
public void onFailure() {
//
}
});
return res ;
}
@Override
protected void onPostExecute() {
//
}
}
我想将“res”分配给“某物”,女巫在 onResponse 方法的回调中。在这段代码中,不可能在 onResponse 方法中分配 res 。
欢迎任何帮助。
谢谢你:)
我的原始代码:我想分配“url”;
private class GetBeaconInfosTask extends AsyncTask<String, Void, Call<Url>> {
Url url ;
@Override
protected Call<Url> doInBackground(String... urls) {
ProxService service = ProxService.Factory.makeProxService(ProxService.ENDPOINT);
return service.getUrlDetails(urls[0]);
}
// onPostExecute return the results of the AsyncTask.
@Override
protected void onPostExecute(Call<Url> call) {
call.enqueue(new Callback<Url>() {
@Override
public void onResponse(Call<Url> call, Response<Url> response) {
url = response.body();
}
@Override
public void onFailure(Call<Url> call, Throwable t) {
//
}
});
if(url == null){
Log.i("url is null", "url is null !!!!! ....");
}
else {
setShopLogo(url.icon) ;
setShopName(url.title);
setDescription(url.description);
setlongUrl(url.longUrl); }
}
}
【问题讨论】:
-
为什么不可能?因为它不是最终的?您可以通过使用 SomeResult 的最终数组并在数组的第一行中分配值来作弊。 Android Studio 应该会向您推荐这个解决方案。
-
你直接返回 res
onResponse怎么样,你不需要存储它。
标签: java android android-asynctask callback asynccallback