android-volley icon indicating copy to clipboard operation
android-volley copied to clipboard

Caching issue

Open WasimMemon opened this issue 10 years ago • 4 comments

Volley stores data with - integer prefix so data caching is not working... volley stores like this - 1:http://abcd/index.php?option=com_ijoomeradv my original url - http://abcd/index.php?option=com_ijoomeradv so when i try to get data from the caching it always returns null

public static void createJsonRequest(final Context ctx, final int apiCode,
                                         String url, final Object dataModel, final ApiResponse listner,
                                         boolean IsProgressDialogRequired)
    {

        Log.d(TAG, "URL:-" + url);
        if (Common.isConnectivityAvailable(ctx)) {
            final ProgressDialog pd = Common.showProgressDialog(ctx, ctx.getResources()
                    .getString(R.string.msg_wait));

            if (IsProgressDialogRequired) {
                pd.show();
            }


            final Gson gson = new GsonBuilder().disableHtmlEscaping().serializeNulls().create();

            StringRequest jsonObjRequest = new StringRequest(Request.Method.POST,
                    url,
                    new Response.Listener() {
                        @Override
                        public void onResponse(String response) {

                            if (pd.isShowing())
                                pd.dismiss();

                            System.out.println("Response :"+response);
                            if (listner != null)
                                listner.NetworkRequestCompleted(apiCode,response);
                        }
                    }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("volley", "Error: " + error.getMessage());
                    error.printStackTrace();

                    if (pd.isShowing())
                        pd.dismiss();

                    if (listner != null)
                        listner.responseError(apiCode,error.getMessage());

                }
            }) {

                @Override
                public String getBodyContentType() {
                    return "application/x-www-form-urlencoded; charset=UTF-8";
                }

                @Override
                protected Map getParams() throws AuthFailureError {
                    Map params = new HashMap();
                    params.put("reqObject", gson.toJson(dataModel));

                    System.out.println("Request :" + params.toString());
                    return params;
                }

                @Override
                protected Response parseNetworkResponse(NetworkResponse response) {

                    Map headers = response.headers;
                    long serverDate = 0;
                    String serverEtag = null;
                    String headerValue;

                    headerValue = headers.get("Date");
                    if (headerValue != null) {
                        serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
                    }

                    serverEtag = headers.get("ETag");

                    // Create a FakeCache that invalidate the data after 24 hour
                    Cache.Entry mFakeCache = HttpHeaderParser.parseCacheHeaders(response);

                    if (mFakeCache == null) {
                        mFakeCache = new Cache.Entry();
                    }

                    mFakeCache.etag = serverEtag;
                    mFakeCache.data = response.data;
                    mFakeCache.softTtl = System.currentTimeMillis() + 86400 * 1000;
                    mFakeCache.ttl = mFakeCache.softTtl;
                    mFakeCache.serverDate = serverDate;

                    String jsonString = "";

                    try {
                        jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    return Response.success(jsonString, mFakeCache);
                }
            };

            RetryPolicy retryPolicy = new DefaultRetryPolicy(CONNECTION_TIMEOUT,1,1f);
            jsonObjRequest.setRetryPolicy(retryPolicy);
            jsonObjRequest.setShouldCache(true);

            if(AppConfig.getAppInstance().getRequestQueue().getCache().get(url) != null){
                //response exists
                String cachedResponse = new String(AppConfig.getAppInstance().getRequestQueue().getCache().get(url).data);
                //results.setText("From Cache: " + cachedResponse);
                if (listner != null)
                {
                    Log.d(TAG,"data from cache...");
                    listner.NetworkRequestCompleted(apiCode,cachedResponse);
                }
            }else{
                //no response
                //queue.add(stringRequest);
                AppConfig.getAppInstance().addToRequestQueue(jsonObjRequest);
            }


        } else {
            String url1 = "1:http://abcd/index.php?option=com_ijoomeradv";
            if(AppConfig.getAppInstance().getRequestQueue().getCache().get(url1) != null){

                //response exists
                String cachedResponse = new String(AppConfig.getAppInstance().getRequestQueue().getCache().get(url1).data);
                //results.setText("From Cache: " + cachedResponse);

                if (listner != null)
                {
                    Log.d(TAG,"data from cache");
                    listner.NetworkRequestCompleted(apiCode,cachedResponse);
                }
            }

            Common.showToast(ctx, ctx.getResources().getString(R.string.msg_noInternet));
        }

WasimMemon avatar Oct 18 '15 08:10 WasimMemon

volley stores like this - 1:http://abcd/index.php?option=com_ijoomeradv my original url - http://abcd/index.php?option=com_ijoomeradv They are the same url, have any problem?

Spring-Xu avatar Oct 22 '15 13:10 Spring-Xu

@CJstar Yes, It appends numbers with url so when i want to retrieve data it always return null because i try to get it without prefix. At run time i don't know what prefix it will append so ? I know about this because i have checked cache file using root explorer.

WasimMemon avatar Oct 22 '15 14:10 WasimMemon

@WasimMemon cacheKey = method+":"+url

public String getCacheKey() {
        return mMethod + ":" + mUrl;
    }

public interface Method {
        int DEPRECATED_GET_OR_POST = -1;
        int GET = 0;
        int POST = 1;
        int PUT = 2;
        int DELETE = 3;
        int HEAD = 4;
        int OPTIONS = 5;
        int TRACE = 6;
        int PATCH = 7;
    }

mcxiaoke avatar Oct 23 '15 02:10 mcxiaoke

Had same problem as @WasimMemon, @mcxiaoke is correct!

mikylucky avatar Jan 31 '16 16:01 mikylucky