Java 在改装呼叫的标头中设置授权令牌后出现未经授权的错误
我正在使用okhttp3的改型2。这里我设置了我的令牌,但仍然显示未经授权的错误。它没有进入下面代码的try-catch中。(即,令牌未被打印) 我的方法是使用单个文件进行改装服务,它将用于所有其他调用 这是我的密码Java 在改装呼叫的标头中设置授权令牌后出现未经授权的错误,java,android,android-studio,retrofit2,rx-java2,Java,Android,Android Studio,Retrofit2,Rx Java2,我正在使用okhttp3的改型2。这里我设置了我的令牌,但仍然显示未经授权的错误。它没有进入下面代码的try-catch中。(即,令牌未被打印) 我的方法是使用单个文件进行改装服务,它将用于所有其他调用 这是我的密码 public static Retrofit getClient() { Gson gson = new GsonBuilder() .setLenient() .create(); O
public static Retrofit getClient() {
Gson gson = new GsonBuilder()
.setLenient()
.create();
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
clientBuilder.addInterceptor(loggingInterceptor);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiConstant.SERVER_API)
.client(createOkHttpClient())
.client(clientBuilder.build())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return retrofit;
}
private static OkHttpClient createOkHttpClient() {
final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
if (isTokenRequired()) {
if (!TextUtils.isEmpty(Profile.getToken())) {
String token = Profile.getToken();
httpClient.addInterceptor(
new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request newRequest = null;
try {
System.out.println("token ==" + token);
newRequest = chain.request().newBuilder()
.header("Authorization", "Token " + token)
.build();
System.out.println("newRequest ===" + newRequest);
} catch (Exception e) {
e.printStackTrace();
}
return chain.proceed(newRequest);
}
});
} else {
throw new IllegalStateException("No Token");
}
}
return httpClient.build();
}
它是授权头(没有这样的授权类型)。基于令牌的身份验证的格式为
承载
,而不是令牌
还要检查您的拦截器是否已实际添加,这应该与指定的更正一起工作。在删除HttpLoggingInterceptor后,拦截器工作
public static Retrofit getClient() {
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiConstant.SERVER_API)
.client(createOkHttpClient())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return retrofit;
}
private static OkHttpClient createOkHttpClient() {
final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
if (isTokenRequired()) {
if (!TextUtils.isEmpty(Profile.getToken())) {
String token = Profile.getToken();
httpClient.addInterceptor(
new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request newRequest = null;
try {
System.out.println("token ==" + token);
newRequest = chain.request().newBuilder()
.header("Authorization", "Token " + token)
.build();
System.out.println("newRequest ===" + newRequest);
} catch (Exception e) {
e.printStackTrace();
}
return chain.proceed(newRequest);
}
});
} else {
throw new IllegalStateException("No Token");
}
}
return httpClient.build();
}
我的授权标头仅为令牌。是否单独尝试?另外,检查是否添加了拦截器。单独表示,是否需要单独添加标头拦截器。我不明白你用了两个OkHttp构建器,一个有HttpLoggingInterceptor,另一个有你的令牌Interceptor。检查问题中的代码,有两个
client()
调用,其中第二个调用覆盖第一个调用(带有日志记录的调用)Yes-it覆盖。所以你能让我知道一件事吗,如果我使用HttpLoggingInterceptor,那么客户端调用就不能像预期的那样工作。那么我如何制作或打印日志呢