Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call.enqueue - Api endpoint is called 2 times implicitly #1242

Closed
xaxist opened this issue Oct 28, 2015 · 7 comments
Closed

call.enqueue - Api endpoint is called 2 times implicitly #1242

xaxist opened this issue Oct 28, 2015 · 7 comments

Comments

@xaxist
Copy link

xaxist commented Oct 28, 2015

        Log.d(TAG, "Calling REST");        

        Call<JsonObject> call = RestClient.getApiService().registerUser();
        call.enqueue(new Callback<JsonObject>()
        {
            @Override
            public void onResponse(Response<JsonObject> response, Retrofit retrofit)
            {
                Log.d(TAG, "REST Response : " + response.body());
            }

            @Override
            public void onFailure(Throwable t)
            {
                Log.e(TAG, "REST Error : " + t.getMessage());
            }
       });

Output in Android Studio

  1. Calling REST
  2. REST Response : {"status":"success"}

If I use Postman (REST Client) to call the api endpoint, its called only once. But with the above code, when executed once, the api endpoint is called twice.

I am trying to format the received data in normal com.google.gson.JsonObject as I don't need the hassles of maintaing Models in Android code.

What's more weird is, first 'Calling REST' gets onto console. Then 2 requests are made and only after second request, 'REST Response : {"status":"success"}' gets onto console.

I am using Retrofit 2.0.0-beta2.

Edit - My Bad, the interceptor fired another request, hence it was being called 2 times.

        OkHttpClient client = new OkHttpClient();
        client.interceptors().add(new Interceptor()
        {
            @Override
            public Response intercept(Chain chain) throws IOException
            {
                Response response = chain.proceed(chain.request());

                // Do anything with response here

                Request newRequest = chain.request().newBuilder().addHeader("User-Agent", Constants.APP_NAME).build();
                return chain.proceed(newRequest);
            }
        });

@xaxist xaxist closed this as completed Oct 28, 2015
@shahsurajk
Copy link

Had the same issue. Thank you so much for posting it here.

@anantrao07
Copy link

What did you do to make only one request?
with Interceptor on

@shahsurajk
Copy link

I had an HTTPLogger and chain.proceed(..) in one interceptor itself. So assuming that HTTPLogger internally also calls chain.proceed() which thus makes two requests, I added a networkinterceptor and moved the HTTPLogger to the network interceptor and my custom apikey chain.proceed(...) In other interceptor. Which worked as a charm and the API was called only once internally. Thus you need to look for code which can possibly call the chain.proceed(..) method internally. Which makes a new request.

@The-RobinHood
Copy link

#2761
Can anybody shade the light? having same issue.

@dbsaini36
Copy link

Can any body post code Solution for same

@3bdoelnaggar
Copy link

any who come after, the solution for me was to remove chain.proceed calls leave only the one that add the header
fun createHttpClientBuilder(loggingInterceptor: HttpLoggingInterceptor, context: Context): OkHttpClient.Builder {
val httpClient = OkHttpClient.Builder()
.connectTimeout(ConfigurationFile.NetworkSettings.REQUEST_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(ConfigurationFile.NetworkSettings.REQUEST_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(ConfigurationFile.NetworkSettings.REQUEST_TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
// .addInterceptor(AuthorizationInterceptorImpl(context))
.addInterceptor { chain: Interceptor.Chain ->
val original = chain.request()
//val response = chain.proceed(original)
val requestBuilder = original.newBuilder()
.addHeader(context.resources.getString(R.string.accept), ConfigurationFile.NetworkSettings.CONTENT_TYPE)
.addHeader(context.resources.getString(R.string.content_type), ConfigurationFile.NetworkSettings.CONTENT_TYPE)
.addHeader("fireBaseToken", fireBaseToken.value.firebaseToken)
val request = requestBuilder.build()
chain.proceed(request)
}
return httpClient

long story short chain.proceed create another request maybe

@agubha
Copy link

agubha commented Mar 30, 2020

I had an HTTPLogger and chain.proceed(..) in one interceptor itself. So assuming that HTTPLogger internally also calls chain.proceed() which thus makes two requests, I added a networkinterceptor and moved the HTTPLogger to the network interceptor and my custom apikey chain.proceed(...) In other interceptor. Which worked as a charm and the API was called only once internally. Thus you need to look for code which can possibly call the chain.proceed(..) method internally. Which makes a new request.

@shahsurajk Thanks save me a lot of trouble

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants