我做一個Android應用程序與改造,我的問題是我需要返回從回調的response.body(),這是我的代碼:改造2異步調用返回響應
public class RoundListProvider {
//Load Rounds
private static RoundsInterface roundClient = RetroBuilder.createService(RoundsInterface.class);
private static RoundList mRoundList;
public static RoundList getRoundList(final Context mContext)
{
Toast.makeText(mContext, "We're in", Toast.LENGTH_LONG).show();
//Fetch info from rounds
Call<RoundList> call = roundClient.getRounds();
call.enqueue(new Callback<RoundList>() {
@Override
public void onResponse(Response<RoundList> response, Retrofit retrofit) {
Log.d("BODY", response.body().toString());
Log.d("MESSAGE", response.message());
Toast.makeText(mContext, "Conexión al servidor hecha con exito", Toast.LENGTH_LONG).show();
mRoundList = response.body();
}
@Override
public void onFailure(Throwable t) {
Log.d("BODY", t.getMessage());
Log.d("ERRRO", t.getStackTrace().toString());
}
});
return mRoundList;
}
}
正如你可以想象的那樣,返回的mRoundList總是爲空,因爲從不等待響應完成,有人可以幫我解決這個問題嗎?
幫助你到底是什麼? – Blackbelt
如果您需要整個響應主體,那麼爲什麼不使用OKHttp呢? – jonathanrz