Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2.0.0 Beta2 OkHttpClient interceptor throwing StackOverFlowError

I am trying to build an interceptor for logging in. Here is my following code for that:

OkHttpClient client = new OkHttpClient();
        client.interceptors().add(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                String authToken = SharedPrefsManager.get(context).getAccessToken();
                request.newBuilder()
                        .addHeader("Authorization", "Bearer " + authToken)
                        .addHeader("Content-Type", "application/json")
                .build();


                return intercept(chain);
            }
        });

My GSON:

  Gson gson = new GsonBuilder()
                .setExclusionStrategies(new ExclusionStrategy() {
                    @Override
                    public boolean shouldSkipField(FieldAttributes f) {
                        return f.getDeclaringClass().equals(RealmObject.class);
                    }

                    @Override
                    public boolean shouldSkipClass(Class<?> clazz) {
                        return false;
                    }
                })
                .create();

My Retrofit RestAdapter:

   Retrofit restAdapter = new Retrofit.Builder()
                .baseUrl(Endpoints.ENDPOINT_BASE_URL+Endpoints.ENDPOINT_VERSION)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();

        apiInterface = restAdapter.create(ApiService.class);

However when I call this service using:

            try {
                    Account myAccount = ApiInterface.getKidMixClient(getActivity()).getAccountDetails().execute().body();
                    callbacks.showDashboard(myAccount.getUser());
                } catch (IOException e) {
                    e.printStackTrace();
                }

I receive a StackOverFlowError, the following Logcat output:

    java.lang.StackOverflowError: stack size 8MB
10-08 16:42:39.013 21279-21279/com.kidmixapp.kidmixchild E/AndroidRuntime:     at java.util.ArrayList.<init>(ArrayList.java:71)
10-08 16:42:39.013 21279-21279/com.kidmixapp.kidmixchild E/AndroidRuntime:     at com.squareup.okhttp.Headers$Builder.<init>(Headers.java:215)
10-08 16:42:39.013 21279-21279/com.kidmixapp.kidmixchild E/AndroidRuntime:     at com.squareup.okhttp.Headers.newBuilder(Headers.java:121)
10-08 16:42:39.013 21279-21279/com.kidmixapp.kidmixchild E/AndroidRuntime:     at com.squareup.okhttp.Request$Builder.<init>(Request.java:137)
10-08 16:42:39.013 21279-21279/com.kidmixapp.kidmixchild E/AndroidRuntime:     at com.squareup.okhttp.Request$Builder.<init>(Request.java:120)
10-08 16:42:39.013 21279-21279/com.kidmixapp.kidmixchild E/AndroidRuntime:     at com.squareup.okhttp.Request.newBuilder(Request.java:94)
10-08 16:42:39.013 21279-21279/com.kidmixapp.kidmixchild E/AndroidRuntime:     at com.kidmixapp.commoncode.networking.ApiInterface$1.intercept(ApiInterface.java:39)
10-08 16:42:39.013 21279-21279/com.kidmixapp.kidmixchild E/AndroidRuntime:     at com.kidmixapp.commoncode.networking.ApiInterface$1.intercept(ApiInterface.java:45)
10-08 16:42:39.013 21279-21279/com.kidmixapp.kidmixchild E/AndroidRuntime:     at com.kidmixapp.commoncode.networking.ApiInterface$1.intercept(ApiInterface.java:45)
like image 905
Timothy Frisch Avatar asked Sep 13 '26 06:09

Timothy Frisch


1 Answers

You need to be using chain.proceed(request), what you're doing is recursively calling the same intercept method.

like image 76
Tyler Sebastian Avatar answered Sep 14 '26 20:09

Tyler Sebastian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!