Modernization: how to wait for an answer

I have an AsyncTask and a doInBackground method, inside which I send a POST request using Retrofit. My code looks like this:

//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; } 

My problem is this: reinstall the send request asynchronously and during its execution the doInBackground method returns a value. Therefore, I need to send the request in the same thread as all the execution in the sequence. One by one. And the return from doInBackground occurs after the request is completed. How to send a request in the same thread using Retrofit?

+5
source share
2 answers

The Call class has an execute() method that will make your call synchronously.

enqueue() explicitly used to create an asynchronous call.

+7
source

"Asynchronously forward the send request", which, as indicated by @ Tanis.7x, makes enqueue () asynchronous, so what could be the reason for installing AsyncTask? (asynchronously in async?)

You can simply output all the modified code from AsyncTask, and onResponse is a callback waiting for the return of your request, so you can perform any user interface update inside this callback.

0
source

Source: https://habr.com/ru/post/1243896/


All Articles