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”赋值给“something”,witch位于onResponse方法的回调中。在这段代码中,不可能在onResponse方法中分配res。将外部变量赋值给AsynckTask中的回调结果
请任何帮助,欢迎。
谢谢:)
我原来的代码:我想指定 “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'你不需要存储它。 –