改造:如何等待响应
我有AsyncTask和doInBackground方法,其中,我使用Retrofit发送POST请求。我的代码看起来像这样:
//method of AsyncTask
protected Boolean doInBackground(Void... params) {
Retrofit restAdapter = new Retrofit.Builder()
.baseUrl(Constants.ROOT_API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
IConstructSecureAPI service = restAdapter.create(IConstructSecureAPI.class);
//request
Call<JsonElement> result = service.getToken("TestUser", "pass", "password");
result.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
}
@Override
public void onFailure(Call<JsonElement> call, Throwable t) {
}
});
return true;
}
问题是:改造异步发送请求,同时,doInBackground方法返回值。因此,我需要在同一线程中发送一个请求,其中包含序列中的所有执行。一个接一个。从 doInBackground 返回发生在请求完成后。如何使用改造在同一线程中发送请求?