我正在使用的 API 在使用 POST 请求成功创建资源时会返回 308?我如何使用 Retrofit2 最好地处理这个问题?
或者 API 不应该返回 308?
最佳答案
如下所示创建 Interceptor
,然后调用 addInterceptor(getRedirectInterceptor())
(在 OkHttpClient.Builder
实例上)
protected Interceptor getRedirectInterceptor() {
return new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
okhttp3.Response response = chain.proceed(request);
if (response.code() == 308) {
request = request.newBuilder()
.url(response.header("Location"))
.build();
response = chain.proceed(request);
}
return response;
}
};
}
关于java - 如何在 Retrofit2 中正确处理重定向作为响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50438655/