Retrofit - HTTP FAILED:java.lang.IllegalStateException:当我尝试刷新令牌时关闭

时间:2018-02-12 08:13:23

标签: java android token retrofit2

我需要通过Retrofit API刷新令牌。我想在Interceptor中做到这一点。但是当我运行应用程序时,它无法完成请求。它返回HTTP FAILED:java.lang.IllegalStateException:closed。 我已经研究了很多信息,但我找不到解决方案。

我的RestClient

public class RestClient {

/* Singleton init */
private static RestClient instance = new RestClient();

public static RestClient getInstance() {
    return instance;
}

private final String TOKEN_PREFIX = "JWT ";
private String token;

public void setToken(String token) {

    this.token = token;
    api = create();
}

public void refreshToken(String token) {
    this.token = token;
}

public String getToken() {
    return token;
}

public String getTokenHeader() {
    return TOKEN_PREFIX + token;
}

private API api;

private RestClient() {
    api = create();
}

public API getApi() {
    return api;
}

private API create() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .connectTimeout(1, TimeUnit.MINUTES)
            .writeTimeout(1, TimeUnit.MINUTES)
            .readTimeout(1, TimeUnit.MINUTES);


    if (token != null) {
        clientBuilder.addInterceptor(new AuthInterceptor(this, getTokenHeader()));
    }
    OkHttpClient client = clientBuilder.build();
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BuildConfig.API_URL)
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
    return retrofit.create(API.class);
}}

我的AuthInterceptor。我用它来进行令牌刷新

public class AuthInterceptor implements Interceptor {
private String authValue;
private RestClient restClient;

public AuthInterceptor(RestClient restClient, String authValue) {
    this.restClient = restClient;
    this.authValue = authValue;
}


@Override
public Response intercept(Chain chain) throws IOException {
    Request original = chain.request();

    Request.Builder requestBuilder = original.newBuilder()
            .header("Authorization", authValue);

    Request request = requestBuilder.build();
    Response response = chain.proceed(request);
    if (response.body().string().equals("jwt expired")) {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.fbToken = AccessToken.getCurrentAccessToken().getToken();
        Call<LoginResponse> call = restClient.getApi().refreshToken(loginRequest);
        restClient.refreshToken(call.execute().body().token);
        Request newRequest = chain.request();
        newRequest = newRequest.newBuilder()
                .header("Authorization", restClient.getTokenHeader()).build();

        response = chain.proceed(newRequest);
    }
    return response;

}}

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

ResponseBody responseBodyCopy = response.peekBody(Long.MAX_VALUE);
responseBodyCopy.string();

对我有用