ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – Retrofit响应缓存

2019-08-28 12:25:36  阅读:229  来源: 互联网

标签:android retrofit2 http retrofit okhttp


这里有很多关于Retrofit缓存的复杂答案,但似乎都没有帮助我.

如何缓存来自Retrofit响应的代码响应?

我的应用程序可以正常下载下面的JSON,但我需要为它做一个缓存,我从来没有在Android上进行缓存.

我需要离线打开应用程序并从我的Retrofit调用中获取结果,就好像我在线一样.

public class APIClient {

public static final String BASE_URL = "https://raw.githubusercontent.com/";
private static Retrofit retrofit = null;

public static Retrofit getClient() {
    if (retrofit==null) {
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Interceptor.Chain chain) throws IOException {
                Request original = chain.request();

                Request request = original.newBuilder()
                        .method(original.method(), original.body())
                        .build();
                return chain.proceed(request);
            }
        });

        OkHttpClient client = httpClient.build();
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }
    return retrofit;
}}

json响应非常简单:

{  
   "fruits":[  
      {  
         "name":"Apple",
         "image":"https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Red_Apple.jpg/265px-Red_Apple.jpg",
         "price":35
      },
      {  
         "name":"Banana",
         "image":"https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Bananas_white_background_DS.jpg/320px-Bananas_white_background_DS.jpg",
         "price":12
      },
      {  
         "name":"Grapes",
         "image":"https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Table_grapes_on_white.jpg/320px-Table_grapes_on_white.jpg",
         "price":45
      },
      {  
         "name":"Pineapple",
         "image":"https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Pineapple_and_cross_section.jpg/286px-Pineapple_and_cross_section.jpg",
         "price":200
      },
      {  
         "name":"cherry",
         "image":"http://www.desicomments.com/wp-content/uploads/2017/05/Cherry-Image-600x570.jpg",
         "price":13
      },
      {  
         "name":"clementine",
         "image":"http://www.icecreamnation.org/wp-content/uploads/2013/04/Clementine_orange.jpg",
         "price":12.4
      },
      {  
         "name":"olive",
         "image":"https://www.homenaturalcures.com/wp-content/uploads/olive.jpg",
         "price":9.5
      },
      {  
         "name":"tomato",
         "image":"http://cdn2.stylecraze.com/wp-content/uploads/2013/05/tomato-hair-benefits1.jpg",
         "price":8.75
      },
      {  
         "name":"huckleberry",
         "image":"http://farm3.static.flickr.com/2131/2082287810_47339fc93e.jpg",
         "price":11.75
      },
      {  
         "name":"papaya",
         "image":"http://media.mercola.com/assets/images/foodfacts/papaya-nutrition-facts.jpg",
         "price":2.75
      },
      {  
         "name":"lime",
         "image":"https://www.florihana.com/images/stories/virtuemart/product/FLE019%20-%20LIME.jpg",
         "price":5.75
      },
      {  
         "name":"pear",
         "image":"https://www.organicfacts.net/wp-content/uploads/pear.jpg",
         "price":4.75
      }
   ]
}

解决方法:

假设服务器遵循Cache-Control / If-Modified-Since标头,那么您所要做的就是创建一个Cache对象并将其设置为OkHttpClient.改造将照顾其余部分.


    int cacheSize = 10 * 1024 * 1024; // 10 MB  
    Cache cache = new Cache(getCacheDir(), cacheSize);

    OkHttpClient okHttpClient = new OkHttpClient.Builder()  
            .cache(cache)
            .build();

    Retrofit.Builder builder = new Retrofit.Builder()  
            .baseUrl("http://10.0.2.2:3000/")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();

资料来源:futurestud.io

标签:android,retrofit2,http,retrofit,okhttp
来源: https://codeday.me/bug/20190828/1751482.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有