I customize an interceptor to refresh token when it's expired. When I get response body to detect if the token's expired, it throw java.lang.IllegalStateException: closed.
Here is my code with OkHttp 3
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Content-Type", "application/json")
.header(Constants.TAG_AUTHORIZATION, "Bearer " + token)
.method(original.method(), original.body());
Request request = requestBuilder.build();
Response response = chain.proceed(request);
if (response.code() == 200) {
String json = response.body().string();
try {
JSONObject obj = new JSONObject(json);
int code = obj.getInt(Constants.TAG_CODE);
if (code == Constants.REQUEST_CODE_TOKEN_EXPIRED) {
Response r = makeTokenRefreshCall(request, chain);
return r;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return response;
}
Please help me!