gpt4 book ai didi

android - OkHttp3 In-None-Match 未发送

转载 作者:行者123 更新时间:2023-11-30 00:33:38 25 4
gpt4 key购买 nike

我正在使用 0kHttp 3.6.0
第一个请求的响应:

05-05 04:54:56.524 W/genymotion_audio: out_write() limiting sleep time 53061 to 39909
05-05 04:54:56.624 D/OkHttp: <-- 200 OK https://example.com/api/v2/products/12?include=variants,brand,option_types.option_values,product_articles (296ms)
05-05 04:54:56.624 D/OkHttp: Connection: close
05-05 04:54:56.624 D/OkHttp: Content-Type: application/json; charset=utf-8
05-05 04:54:56.624 D/OkHttp: Server: Cowboy
05-05 04:54:56.624 D/OkHttp: Date: Fri, 05 May 2017 08:54:59 GMT
05-05 04:54:56.624 D/OkHttp: X-Frame-Options: SAMEORIGIN
05-05 04:54:56.624 D/OkHttp: X-Xss-Protection: 1; mode=block
05-05 04:54:56.624 D/OkHttp: X-Content-Type-Options: nosniff
05-05 04:54:56.624 D/OkHttp: Vary: User-Agent
05-05 04:54:56.624 D/OkHttp: Etag: "75587e81446d816b70a25dbfc602bcab"
05-05 04:54:56.624 D/OkHttp: Last-Modified: Tue, 03 Jan 2017 05:37:37 GMT
05-05 04:54:56.624 D/OkHttp: Access-Control-Allow-Origin: *
05-05 04:54:56.624 D/OkHttp: Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
05-05 04:54:56.624 D/OkHttp: Access-Control-Max-Age: 1728000
05-05 04:54:56.624 D/OkHttp: Access-Control-Allow-Headers: Authorization,Content-Type
05-05 04:54:56.624 D/OkHttp: Cache-Control: max-age=0, private, must-revalidate
05-05 04:54:56.624 D/OkHttp: X-Request-Id: e6a2bdfc-666f-499e-8a4c-07b61199fa78
05-05 04:54:56.624 D/OkHttp: X-Runtime: 0.042622
05-05 04:54:56.624 D/OkHttp: Via: 1.1 vegur

第二个请求:

05-05 04:56:38.652 D/OkHttp: --> GET https://example.com/api/v2/products/12?include=variants,brand,option_types.option_values,product_articles http/1.1
05-05 04:56:38.652 D/OkHttp: X-OS-Name: android
05-05 04:56:38.652 D/OkHttp: X-Platform: mobile_app
05-05 04:56:38.652 D/OkHttp: X-Device-Model: Genymotion Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920
05-05 04:56:38.652 D/OkHttp: X-OS-Version: 4.4.4
05-05 04:56:38.652 D/OkHttp: Accept-Language: en-SG
05-05 04:56:38.652 D/OkHttp: --> END GET

OkHttp 客户端:

    private static Interceptor headerInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
builder.addHeader("X-OS-Name", "android");
builder.addHeader("X-Platform", "mobile_app");
builder.addHeader("X-Device-Model", DeviceUtils.getDeviceModel());
builder.addHeader("X-OS-Version", DeviceUtils.getDeviceVersion());
Request request = builder.build();
return chain.proceed(request);
}
};
private static OkHttpClient getOkHttpClient(Context context) {
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.addInterceptor(headerInterceptor)
.addInterceptor(loggingInterceptor)
.connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS);

int cacheSize = 10 * 1024 * 1024; // 10 MiB
File cacheDir = new File(context.getCacheDir(), "http");
Cache cache = new Cache(cacheDir, cacheSize);
builder.cache(cache);

return builder.build();
}

我读过 this
我不想手动将 Etag 值存储在本地数据存储中并将其附加到匹配请求中,因为这会影响性能。

最佳答案

它已经可以工作了,问题是我使用的日志拦截器没有显示 HTTP 304
(addInterceptor 位于缓存和应用程序之间,如 here 所述)

addInterceptor 更改为 addNetworkInterceptor 揭示了 OkHttp 的魔力:

05-05 05:21:33.436 D/OkHttp: --> GET https://example.com/api/v2/products/12?include=variants,brand,option_types.option_values,product_articles http/1.1
05-05 05:21:33.436 D/OkHttp: X-OS-Name: android
05-05 05:21:33.436 D/OkHttp: X-Platform: mobile_app
05-05 05:21:33.436 D/OkHttp: X-Device-Model: Genymotion Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920
05-05 05:21:33.436 D/OkHttp: X-OS-Version: 4.4.4
05-05 05:21:33.436 D/OkHttp: Accept-Language: en-SG
05-05 05:21:33.436 D/OkHttp: Host: example.herokuapp.com
05-05 05:21:33.436 D/OkHttp: Connection: Keep-Alive
05-05 05:21:33.436 D/OkHttp: Accept-Encoding: gzip
05-05 05:21:33.436 D/OkHttp: User-Agent: okhttp/3.6.0
05-05 05:21:33.436 D/OkHttp: If-None-Match: "75587e81446d816b70a25dbfc602bcab"
05-05 05:21:33.436 D/OkHttp: --> END GET
05-05 05:21:33.892 D/OkHttp: <-- 304 Not Modified https://example.com/api/v2/products/12?include=variants,brand,option_types.option_values,product_articles (455ms)
05-05 05:21:33.892 D/OkHttp: Server: Cowboy
05-05 05:21:33.892 D/OkHttp: Content-Length: 0
05-05 05:21:33.892 D/OkHttp: Connection: keep-alive
05-05 05:21:33.892 D/OkHttp: Date: Fri, 05 May 2017 09:23:14 GMT
05-05 05:21:33.892 D/OkHttp: X-Frame-Options: SAMEORIGIN
05-05 05:21:33.892 D/OkHttp: X-Xss-Protection: 1; mode=block
05-05 05:21:33.892 D/OkHttp: X-Content-Type-Options: nosniff
05-05 05:21:33.892 D/OkHttp: Vary: User-Agent
05-05 05:21:33.892 D/OkHttp: Etag: "75587e81446d816b70a25dbfc602bcab"
05-05 05:21:33.892 D/OkHttp: Last-Modified: Tue, 03 Jan 2017 05:37:37 GMT
05-05 05:21:33.892 D/OkHttp: Access-Control-Allow-Origin: *
05-05 05:21:33.892 D/OkHttp: Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
05-05 05:21:33.892 D/OkHttp: Access-Control-Max-Age: 1728000
05-05 05:21:33.892 D/OkHttp: Access-Control-Allow-Headers: Authorization,Content-Type
05-05 05:21:33.892 D/OkHttp: Cache-Control: max-age=0, private, must-revalidate
05-05 05:21:33.892 D/OkHttp: X-Request-Id: 941c04d1-2d5c-44b7-b6ef-82a455d95dc1
05-05 05:21:33.892 D/OkHttp: X-Runtime: 0.053531
05-05 05:21:33.892 D/OkHttp: Via: 1.1 vegur
05-05 05:21:33.892 D/OkHttp: <-- END HTTP (0-byte body)

代码:

private static OkHttpClient getOkHttpClient(Context context) {
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.addInterceptor(headerInterceptor)
.addNetworkInterceptor(loggingInterceptor)
.connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS);

int cacheSize = 10 * 1024 * 1024; // 10 MiB
File cacheDir = new File(context.getCacheDir(), "http");
Cache cache = new Cache(cacheDir, cacheSize);
builder.cache(cache);

return builder.build();
}

关于android - OkHttp3 In-None-Match 未发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43800728/

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