Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
改型-HTTP失败:java.lang.IllegalStateException:尝试刷新令牌时关闭_Java_Android_Token_Retrofit2 - Fatal编程技术网

改型-HTTP失败:java.lang.IllegalStateException:尝试刷新令牌时关闭

改型-HTTP失败:java.lang.IllegalStateException:尝试刷新令牌时关闭,java,android,token,retrofit2,Java,Android,Token,Retrofit2,我需要通过改型API刷新令牌。我想在拦截器里做。但当我运行应用程序时,它无法完成请求。它返回HTTP FAILED:java.lang.IllegalStateException:closed。 我已经研究了很多信息,但我找不到解决办法 我的客户 public class RestClient { /* Singleton init */ private static RestClient instance = new RestClient(); public static RestClie

我需要通过改型API刷新令牌。我想在拦截器里做。但当我运行应用程序时,它无法完成请求。它返回HTTP FAILED:java.lang.IllegalStateException:closed。 我已经研究了很多信息,但我找不到解决办法

我的客户

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);
}}
我的截取器。我用它来刷新令牌

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;

}}
公共类AuthInterceptor实现拦截器{
私有字符串authValue;
私人客户;
公共AuthInterceptor(RestClient RestClient,字符串authValue){
this.restClient=restClient;
this.authValue=authValue;
}
@凌驾
公共响应拦截(链)引发IOException{
Request original=chain.Request();
Request.Builder requestBuilder=original.newBuilder()
.header(“授权”,authValue);
Request=requestBuilder.build();
响应=链。继续(请求);
if(response.body().string().equals(“jwt过期”)){
LoginRequest LoginRequest=新的LoginRequest();
loginRequest.fbToken=AccessToken.getCurrentAccessToken().getToken();
Call Call=restClient.getApi().refreshtToken(loginRequest);
restClient.refreshttoken(call.execute().body().token);
Request newRequest=chain.Request();
newRequest=newRequest.newBuilder()
.header(“Authorization”,restClient.getTokenHeader()).build();
响应=链。继续(新请求);
}
返回响应;
}}
尝试以下操作:

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

它适用于我

您可能需要为新令牌使用验证器。