gpt4 book ai didi

android - HTTP 504 无法满足的请求(仅在缓存时)

转载 作者:行者123 更新时间:2023-12-03 16:41:07 44 4
gpt4 key购买 nike

Retrofit2.2.0 和 okhttp3.9.1 离线缓存不起作用,当我请求离线数据时会抛出一个异常,即 HTTP 504 Unsatisfiable Request (only-if-cached)。数据正在从互联网加载到设备中,但离线模式无法检索它。

示范:
enter image description here

以下代码描述了 API 接口(interface)。

public class TestApi {


private static TestService testService;

private static final String TEST_URL = "https://httpbin.org/";


public static TestService getTestService() {

if(testService == null) {
synchronized (TestApi.class) {
if(testService == null) {
testService = XApi.getInstance().getRetrofit(TEST_URL, true).create(TestService.class);
}
}
}
return testService;
}

}

以下代码描述了 API 服务。
public interface TestService {

@GET("/cache/60")
Flowable<TestBean> getTestDate();

}

下面的代码描述了缓存控制拦截器。
public class CachingControlInterceptor {

private static final int TIMEOUT_CONNECT = 60; //60 second
private static final int TIMEOUT_DISCONNECT = 60 * 60 * 24 * 7; //7天

public static final Interceptor REWRITE_RESPONSE_INTERCEPTOR = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
String cache = chain.request().header("cache");
okhttp3.Response originalResponse = chain.proceed(chain.request());
String cacheControl = originalResponse.header("Cache-Control");
if (cacheControl == null) {
if (cache == null || "".equals(cache)) {
cache = TIMEOUT_CONNECT + "";
}
originalResponse = originalResponse.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, max-age=" + cache)
.build();
return originalResponse;
} else {
return originalResponse;
}
}
};

public static final Interceptor REWRITE_RESPONSE_INTERCEPTOR_OFFLINE = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!NetworkUtils.isConnected()) {
request = request.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached, max-stale="+TIMEOUT_DISCONNECT)
.build();
}
return chain.proceed(request);
}
};

}

最后,在 OkHttpClient.Builder 中添加一个拦截器
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(getClient(baseUrl, provider))
.addConverterFactory(GsonConverterFactory.create());

builder.addNetworkInterceptor(CachingControlInterceptor.REWRITE_RESPONSE_INTERCEPTOR);
builder.addInterceptor(CachingControlInterceptor.REWRITE_RESPONSE_INTERCEPTOR_OFFLINE);

而且我不知道如何解决。

希望有人可以帮助我!

最佳答案

我仍然不太了解您到底要做什么,但是从一个更简单的可执行示例开始,以下有什么问题?

package com.baulsupp.oksocial;

import okhttp3.*;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.http.GET;

import java.io.File;
import java.io.IOException;

public class TestRequest {
private static boolean connected = true;

public interface TestService {
@GET("/cache/60")
Call<String> getTestDate();
}

public static final Interceptor REWRITE_RESPONSE_INTERCEPTOR_OFFLINE = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (isConnected()) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();
} else {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);

System.out.println("network: " + response.networkResponse());
System.out.println("cache: " + response.cacheResponse());

return response;
}
};

private static boolean isConnected() {
return connected;
}

public static void main(String[] args) throws IOException {

OkHttpClient.Builder clientBuilder =
new OkHttpClient.Builder().cache(new Cache(new File("/tmp/http"), 10 * 1024 * 1024));

clientBuilder.addInterceptor(REWRITE_RESPONSE_INTERCEPTOR_OFFLINE);

Retrofit builder = new Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.baseUrl("https://httpbin.org/")
.client(clientBuilder.build())
.build();

TestService service = builder.create(TestService.class);

connected = true;

String online = service.getTestDate().execute().body();
System.out.println(online);

connected = false;

String offline = service.getTestDate().execute().body();
System.out.println(offline);
}
}

输出
network: Response{protocol=http/1.1, code=200, message=OK, url=https://httpbin.org/cache/60}
cache: null
{
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Cache-Control": "no-cache",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "okhttp/3.9.1"
},
"origin": "82.5.95.16",
"url": "https://httpbin.org/cache/60"
}

network: null
cache: Response{protocol=http/1.1, code=200, message=OK, url=https://httpbin.org/cache/60}
{
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Cache-Control": "no-cache",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "okhttp/3.9.1"
},
"origin": "82.5.95.16",
"url": "https://httpbin.org/cache/60"
}

关于android - HTTP 504 无法满足的请求(仅在缓存时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48057855/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com