Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Caching issue #109

Open
WasimMemon opened this issue Oct 18, 2015 · 4 comments
Open

Caching issue #109

WasimMemon opened this issue Oct 18, 2015 · 4 comments

Comments

@WasimMemon
Copy link

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));
        }
@Spring-Xu
Copy link

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?

@WasimMemon
Copy link
Author

@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.

@mcxiaoke
Copy link
Owner

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

@mikylucky
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants