Json解析使用Volley不会被缓存

时间:2014-12-01 13:28:00

标签: json parsing caching android-volley

我使用volley框架解析json,每次都从服务器获得响应,不检查缓存,它花了一整天,这是我的代码。你们中的任何人都使用过排球来解析json有望帮助

    Cache cache = AppController.getInstance().getRequestQueue().getCache();
    Entry entry = cache.get(diag_url);
    if(entry != null){
        try {
            String data = new String(entry.data, "UTF-8");
            // handle data, like converting it to xml, json, bitmap etc.,
            // Parsing json
            JSONArray jsonArray = new JSONArray(data);
            for (int i = 0; i < jsonArray.length(); i++) {
                try { 
                    DiagRegPojo test = new DiagRegPojo();                       
                    JSONObject obj = jsonArray.getJSONObject(i);
                    String testName = obj.getString("content");  
                    Log.d("Response From Cache", testName);
                    test.setTitle(testName); 
                    // adding movie to movies array
                    testList.add(test);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } 
        } catch (UnsupportedEncodingException e) {      
            e.printStackTrace();
            } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        }else{


    // Creating volley request obj
    JsonArrayRequest testReq = new JsonArrayRequest(diag_url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try { 
                            JSONObject obj = response.getJSONObject(i);
                            DiagRegPojo test = new DiagRegPojo();
                            test.setTitle(obj.getString("content")); 
                            Log.d("Response From Server", obj.getString("content"));
                            // adding movie to movies array
                            testList.add(test);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } 
                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    mAdapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }
            })
        {            
        //**
         // Passing some request headers
          //*
         @Override
         public Map<String, String> getHeaders() throws AuthFailureError {
             HashMap<String, String> headers = new HashMap<String, String>();
             headers.put("Cookie", MainActivity.sharedpreferences.getString(savedCookie, ""));                
             headers.put("Set-Cookie", MainActivity.sharedpreferences.getString(savedCookie, ""));
             headers.put("Content-Type", "application/x-www-form-urlencoded");                
             //headers.put("Content-Type","application/json");
             headers.put("Accept", "application/x-www-form-urlencoded");                
             return headers;
         } 
     };         
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(testReq);
}        
}

1 个答案:

答案 0 :(得分:0)

要缓存图片,我已经使用过了。确定它对你有所帮助。

public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        mImageLoader = new ImageLoader(this.mRequestQueue,
                new LruBitmapCache());
    }
    return this.mImageLoader;
}

public class LruBitmapCache extends LruCache<String, Bitmap> implements
    ImageCache {
public static int getDefaultLruCacheSize() {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;

    return cacheSize;
}

public LruBitmapCache() {
    this(getDefaultLruCacheSize());
}

public LruBitmapCache(int sizeInKiloBytes) {
    super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
    return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
    put(url, bitmap);
}

}