6

OkHttp と Retrofit を使用して http リクエストをキャッシュしようとしています。しかし、なぜそれが機能していないのか理解できないようです。

@Headers("Cache-Control: public, max-age=640000, s-maxage=640000 , max-stale=10000000")
    @FormUrlEncoded
    @POST("/news/getNewslist/")
    void newsListByGenre(@Field("news_genre") String genre,
            Callback<ArrayList<NewsStory>> callback);

これはリクエストの 1 つで、必要なヘッダーがすべて含まれています。さらに、何かがファイル キャッシュに書き込まれることをテストするために、OkHttpClient に手動でキャッシュを割り当てました。

OkHttpClient name = new OkHttpClient();

            try {
                if (!cache.exists())
                    cache.createNewFile();
                name.setResponseCache(new HttpResponseCache(cache,
                        10 * 1024 * 1024));
            } catch (IOException e) {
                e.printStackTrace();
            }

私が作成したファイル キャッシュは 36 バイトしかないので、何もキャッシュされていないことは確かです。

I have also tried to make sure that the server has required headers, although I want it to work without server interference but I set the cache control headers in the request as well. This is the debug log from retrofit.

null: HTTP/1.1 200 OK
Cache-Control: public, max-age=360000
Connection: Keep-Alive
Content-Length: 5167
Content-Type: application/json
Date: Fri, 28 Jun 2013 01:00:22 GMT
Keep-Alive: timeout=5, max=99
Server: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
X-Android-Received-Millis: 1372381311315
X-Android-Response-Source: NETWORK 200
X-Android-Selected-Transport: http/1.1
X-Android-Sent-Millis: 1372381311048
X-Powered-By: PHP/5.4.7

I have read the caching mechanism of http again and again but seems I am missing something.

4

2 に答える 2

0

@Jesse Wilson-時々応答をキャッシュするのは理にかなっていると思います-ネットワーク呼び出しを行うアプリに取り組んでいますが、ユーザーがアクティビティを切り替えて戻ってきた場合、以前に POST 応答によって返されたのと同じデータを使用したいと思います。私はORMを使用してmuオブジェクトなどを永続化できることを知っていますが、レトロフィットに同じ応答を使用するように指示する方がはるかに簡単であるように思えます。

于 2013-11-21T22:58:03.560 に答える