volley json cache image doesn't work offline

后端 未结 1 1696
刺人心
刺人心 2021-01-16 19:09

Hello i\'m beginner in anroid. I created navigation drawer and fragments. I can getting data from json with url. It\'s working while online. But phone when offline i\'m gett

相关标签:
1条回答
  • 2021-01-16 19:29

    your pictures(http://www.iremdeveci.com/ornekler/resimler/resim.png) seems to be cached correctly as the have etag and cache headers.

    However the very first request you make (http://sunumsitesi.com/json.php) doesn't seems to return neither cache headers nor etag. Thus you have the error you dont even reach the picture part.

    You can either fix that from the server or make Volley cache it all, for which you need to create custom request and override

    protected Response<String> parseNetworkResponse(NetworkResponse response)
    

    You can check my answer here.

    In your code this would be smth like

            JsonArrayRequest urunReq = new JsonArrayRequest(jsonUrl, new Response.Listener<JSONArray>()
        {
            @Override
            public void onResponse(JSONArray response)
            {
                String dosyakonum = "http://www.pukkaliving.concept.com/upload/galeri/buyuk/";
                hidePDialog();
                for (int i = 0; i < response.length(); i++)
                {
                    try
                    {
    
                        JSONObject obj = response.getJSONObject(i);
                        Urun urun = new Urun();
                        urun.setUrun_adi(obj.getString("urun_adi"));
                        urun.setUrl("http://www.iremdeveci.com/ornekler/resimler/resim.png");//example
                        urunList.add(urun);
                    } catch (JSONException ex)
                    {
                        ex.printStackTrace();
                    }
                }
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError volleyError)
            {
                Toast.makeText(getActivity(), volleyError.getMessage(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString =
                            new String(response.data, HttpHeaderParser.parseCharset(response.headers));
    
                    long now = System.currentTimeMillis();
                    Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
                    entry.ttl = now + 30l * 24 * 60 * 60 * 1000;  //keeps cache for 30 days
    entry.softTtl = now + 1 * 24 * 60 * 60 * 1000;  //will not try to refresh for 1 day     
                        return Response.success(new JSONArray(jsonString), entry);
                    } catch (UnsupportedEncodingException e) {
                        return Response.error(new ParseError(e));
                    } catch (JSONException je) {
                        return Response.error(new ParseError(je));
                    }
                }
            };
            AppController.getmInstance().addToRequestQueue(urunReq);
    

    however for NetworkImage view you have to customize ImageLoader as this is the one creating the image requests and then the logic is the same. You have to override "makeImageRequest".

    ex:

      ...
    mImageLoader = new ImageLoader(this.mRequestQueue,
                        new LruBitmapCache()) {
        @Override
            protected Request<Bitmap> makeImageRequest(String requestUrl, int maxWidth, int maxHeight,
                        ScaleType scaleType, final String cacheKey) {
                    return new ImageRequest(requestUrl, new Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap response) {
                            onGetImageSuccess(cacheKey, response);
                        }
                    }, maxWidth, maxHeight, scaleType, Config.RGB_565, new ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            onGetImageError(cacheKey, error);
                        }
                    }){
              @Override
                        public Response<Bitmap> parseNetworkResponse(NetworkResponse response) {
                            Response<Bitmap> resp = super.parseNetworkResponse(response);
                            if(!resp.isSuccess()) {
                                return resp;
                            }
                            long now = System.currentTimeMillis();
                            Cache.Entry entry = resp.cacheEntry;
                            entry.ttl = now + 30l * 24 * 60 * 60 * 1000;  //keeps cache for 30 days
                            return Response.success(resp.result, entry);
                        }
    
            };
                }
    };
        ...
    
    0 讨论(0)
提交回复
热议问题