我正在尝试构建一个用于登录的拦截器。以下是我的以下代码:
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);
}
});
我的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();
我的改造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);
但是,当我使用以下方式调用此服务时:
try {
Account myAccount = ApiInterface.getKidMixClient(getActivity()).getAccountDetails().execute().body();
callbacks.showDashboard(myAccount.getUser());
} catch (IOException e) {
e.printStackTrace();
}
我收到StackOverFlowError
,以下Logcat
输出:
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)
答案 0 :(得分:4)
您需要使用chain.proceed(request)
,您正在做的是以递归方式调用相同的intercept
方法。