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

Stack Overflow error with Retrofit 2+ okhttp3 client #2001

Closed
bhagyasri opened this issue Aug 31, 2016 · 5 comments
Closed

Stack Overflow error with Retrofit 2+ okhttp3 client #2001

bhagyasri opened this issue Aug 31, 2016 · 5 comments

Comments

@bhagyasri
Copy link

Issue Log:
OkHttp DispatcherFATAL EXCEPTION: OkHttp Dispatcher
Process: com.mallow.xxx.xxx.dev, PID: 28874
java.lang.StackOverflowError
at java.lang.String.startsWith(String.java:1428)
at java.lang.String.startsWith(String.java:1411)
at java.net.URI.isValidHost(URI.java:521)
at java.net.URI.parseAuthority(URI.java:491)
at java.net.URI.parseURI(URI.java:409)
at java.net.URI.(URI.java:204)
at okhttp3.HttpUrl.uri(Unknown)
at okhttp3.internal.http.RouteSelector.resetNextProxy(Unknown)
at okhttp3.internal.http.RouteSelector.(Unknown)
at okhttp3.internal.http.StreamAllocation.(Unknown)
at okhttp3.internal.http.HttpEngine.(Unknown)
at okhttp3.RealCall.getResponse(Unknown)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(Unknown)
at com.mallow.xxx.xxx.retrofit.xxxRestClient.-com_mallow_xxx_xxx_retrofit_xxxRestClient_lambda$1(xxxRestClient.java:89)
at com.mallow.xxx.xxx.retrofit.xxxRestClient$-retrofit2_Retrofit_getRetrofitClient__LambdaImpl0.intercept(xxxRestClient.java)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(Unknown)
at okhttp3.RealCall.getResponseWithInterceptorChain(Unknown)
at okhttp3.RealCall.execute(Unknown)
at retrofit2.OkHttpCall.execute(Unknown)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(Unknown)
at com.mallow.xxx.xxx.retrofit.xxxRestClient$CustomAuthenticator.authenticate(xxxRestClient.java:215)
at okhttp3.internal.http.HttpEngine.followUpRequest(Unknown)
at okhttp3.RealCall.getResponse(Unknown)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(Unknown)
at com.mallow.xxx.xxx.retrofit.xxxRestClient.-com_mallow_xxx_xxx_retrofit_xxxRestClient_lambda$1(xxxRestClient.java:89)
at com.mallow.xxx.xxx.retrofit.xxxRestClient$-retrofit2_Retrofit_getRetrofitClient__LambdaImpl0.intercept(xxxRestClient.java)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(Unknown)
at okhttp3.RealCall.getResponseWithInterceptorChain(Unknown)
at okhttp3.RealCall.execute(Unknown)
at retrofit2.OkHttpCall.execute(Unknown)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(Unknown)
at com.mallow.xxx.xxx.retrofit.xxxRestClient$CustomAuthenticator.authenticate(xxxRestClient.java:215)
at okhttp3.internal.http.HttpEngine.followUpRequest(Unknown)
at okhttp3.RealCall.getResponse(Unknown)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(Unknown)
at com.mallow.xxx.xxx.retrofit.xxxRestClient.-com_mallow_xxx_xxx_retrofit_xxxRestClient_lambda$1(xxxRestClient.java:89)
at com.mallow.xxx.xxx.retrofit.xxxRestClient$-retrofit2_Retrofit_getRetrofitClient__LambdaImpl0.intercept(xxxRestClient.java)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(Unknown)
at okhttp3.RealCall.getResponseWithInterceptorChain(Unknown)
at okhttp3.RealCall.execute(Unknown)
at retrofit2.OkHttpCall.execute(Unknown)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(Unknown)
at com.mallow.xxx.xxx.retrofit.xxxRestClient$CustomAuthenticator.authenticate(xxxRestClient.java:215)
at okhttp3.internal.http.HttpEngine.followUpRequest(Unknown)
at okhttp3.RealCall.getResponse(Unknown)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(Unknown)
at com.mallow.xxx.xxx.retrofit.xxxRestClient.-com_mallow_xxx_xxx_retrofit_xxxRestClient_lambda$1(xxxRestClient.java:89)
at com.mallow.xxx.xxx.retrofit.xxxRestClient$-retrofit2_Retrofit_getRetrofitClient__LambdaImpl0.intercept(xxxRestClient.java)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(Unknown)
at okhttp3.RealCall.getResponseWithInterceptorChain(Unknown)
at okhttp3.RealCall.execute(Unknown)
at retrofit2.OkHttpCall.execute(Unknown)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(Unknown)
at com.mallow.xxx.xxx.retrofit.xxxRestClient$CustomAuthenticator.authenticate(xxxRestClient.java:215)
at okhttp3.internal.http.HttpEngine.followUpRequest(Unknown)
at okhttp3.RealCall.getResponse(Unknown)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(Unknown)
at com.mallow.xxx.xxx.retrofit.xxxRestClient.-com_mallow_xxx_xxx_retrofit_xxxR

@swankjesse
Copy link
Member

Don’t add multiple copies of the same interceptor to the same OkHttpClient instance.

@bhagyasri
Copy link
Author

bhagyasri commented Sep 1, 2016

The below code is used for initiating retrofit client with OkHttpClient. Could you please suggest which you refer for "multiple copies of the same interceptor to the same OkHttpClient instance". Thanks in advance.

Retrofit Initialising Code:
if (retrofitClient == null) {
OkHttpClient okClient = new OkHttpClient.Builder()
.authenticator(new CustomAuthenticator())
.addInterceptor(
chain -> {
Request request = chain.request().newBuilder()
.addHeader("content-type", "application/json")
.build();
return chain.proceed(request);
}).build();
retrofitClient = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}

@swankjesse
Copy link
Member

Maybe I’m mistaken. Is your CustomAuthenticator re-entrant?

@bhagyasri
Copy link
Author

bhagyasri commented Sep 1, 2016

Yes, I also attached the CustomAuthenticator class for your reference.

Custom Authenticator Code:
public static class CustomAuthenticator implements Authenticator {
@OverRide
public Request authenticate(Route route, Response response) throws IOException {
String accessToken = null;
Call authRequestCall = getClient().refreshToken(Utilities.getAuthorizationToken());
retrofit2.Response< RefreshResponse> refreshResponse = null;
try {
refreshResponse = authRequestCall.execute();
if (refreshResponse.isSuccessful()) {
accessToken = refreshResponse.body().getToken();
Log.d(TAG, "Refresh token API success " + accessToken);
} else{
Log.d(TAG, "Refresh token API error " + refreshResponse.code());
}
} catch (Exception exception) {
Log.e("Exception ", exception.getLocalizedMessage());
}
//check if response equals 400 , mean empty response
if (accessToken != null) {
App.getInstance().getPrefs().setToken(accessToken);
//save new access and refresh token
// than create a new request and modify it accordingly using the new token
return response.request().newBuilder()
.header(AUTHORISATION_KEY, App.getInstance().getResources().getString(R.string.bearer) + accessToken)
.build();
} else {
Log.d(TAG, "Refresh token response is null");
BaseActivity.startLoginActivity();
return null;
}
}
}

@swankjesse
Copy link
Member

You should probably not make HTTP calls that use your interceptor from within your interceptor. That’s called infinite recursion and it’s very slow.

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

2 participants