2014-12-01 3 views
2

I Parse json, использующий фреймворк volley, который каждый раз получает ответ от сервера, не проверяет кеш, он занял целый день. Вот мой код. Любой из вас использовал залп для разбора json, как ожидается, поможетJson parsing Использование Volley не получает cahced

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); 
}   
} 

ответ

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); 
} 

}

Смежные вопросы