16

I have AsyncTask and the doInBackground method inside which, I sending POST request using Retrofit. My code looks like:

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

The problem is: Retrofit sending request asynchronously and while it, the doInBackground method returning the value. So I need to send a request in the same thread with all executions in the sequence. One by one. And returning from doInBackground occurs after the request finished. How can I send a request in the same thread using Retrofit?

kkost
  • 3,640
  • 5
  • 41
  • 72

3 Answers3

25

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

enqueue() is explicitly for making an asychronous call.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • 2
    yeah but as execute() is synchronous, its seems to be running on the main thread. In this case execute() is the right answer because already, the poster is working on a background thread and execute() will be on that thread. – Maxime Claude Dec 10 '16 at 19:37
0

"Retrofit sending request asynchronously" which as @Tanis.7x mentioned, enqueue() is doing the asynchronous, so what could be a reason to put in AsyncTask ? (async in async ?)

You can simply put all retrofit code out from AsyncTask and onResponse is the callback that is waiting for your request call to be back, so you can do any UI update inside of this callback.

Emma
  • 8,518
  • 1
  • 18
  • 35
  • I created LoginActivity by Android Studio, and it have TODO login section for sending login request. By default it located inside AsyncTask. I don't want to spend a lot of time for rewriting default activity structure and logic, so I just what to send request in the sequence inside the doInBackground – kkost Feb 25 '16 at 22:36
  • 1
    woah. I've just learned that AS is generating so much helpful code for LoginActivity ! That is insane. What you said about without refactoring makes sense, thanks @neustart47 ! – Emma Feb 26 '16 at 03:09
0

Use retrofit execute method instead of the enqueue method.

Because in asynctask doInbackground method is already creating a thread to execute the code in background. No need to create it again using retrofit enqueue.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
uria
  • 1
  • 1