package cn.ragnarok.monoreader.api.base;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import cn.ragnarok.monoreader.api.util.Constant;
import cn.ragnarok.monoreader.api.util.ErrorHelper;

/**
 * Created by ragnarok on 14-5-21.
 */
public class BaseAPIRequest extends StringRequest {

    protected String mPostData = null;

    public BaseAPIRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, listener, errorListener);
    }

    public BaseAPIRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener, String postData) {
        super(method, url, listener, errorListener);
        this.mPostData = postData;
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        try {
            String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            JSONObject resultObject = new JSONObject(json);
            int errorCode = Integer.parseInt(resultObject.getString("error_code"));
            if (errorCode != Constant.ErrorCode.SUCCESS) {
                return ErrorHelper.handleError(errorCode); // return Response.error here, so that invoke errorListener
            } else {
                String data = resultObject.get("data").toString();
                return Response.success(data, HttpHeaderParser.parseCacheHeaders(response));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return Response.error(new ParseError());
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        if (this.mPostData != null) {
            return this.mPostData.getBytes();
        }
        return super.getBody();
    }
}

@@ -71,32 +71,65 @@
 thFailureError;%0A
+import com.android.volley.Cache;%0A
 import com.andro
@@ -435,16 +435,38 @@
 ception;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -872,24 +872,59 @@
 rListener);%0A
+        this.setShouldCache(true);%0A
     %7D%0A%0A    p
@@ -1149,16 +1149,51 @@
 stData;%0A
+        this.setShouldCache(true);%0A
     %7D%0A%0A 
@@ -1281,24 +1281,52 @@
 response) %7B%0A
+        String json = null;%0A
         try 
@@ -1331,39 +1331,32 @@
 y %7B%0A            
-String 
 json = new Strin
@@ -1418,24 +1418,178 @@
 .headers));%0A
+        %7D catch (UnsupportedEncodingException e) %7B%0A            e.printStackTrace();%0A            json = new String(response.data);%0A        %7D%0A        try %7B%0A
             
@@ -2036,116 +2036,693 @@
 ta, 
-HttpHeaderParser.parseCacheHeaders(response));%0A            %7D%0A        %7D
+makeCacheEntry(response));%0A            %7D%0A        %7D catch (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return Response.error(new ParseError());%0A    %7D%0A%0A    private Cache.Entry makeCacheEntry(NetworkResponse response) %7B%0A        if (this.getMethod() == Method.POST) %7B%0A            return HttpHeaderParser.parseCacheHeaders(response); // not cache post request%0A        %7D else %7B%0A            /**%0A             * custom response
  ca
-t
 ch
- (UnsupportedEncodingException e
+e, ignore %22Cache-Control%22 header%0A             */%0A            long now = System.currentTimeMillis();%0A            Map%3CString, String%3E headers = response.headers;%0A            long serverDate = 0;%0A            if (headers.get(%22Date%22) != null
 ) %7B%0A
@@ -2737,156 +2737,732 @@
     
+   
-e.printStackTrace();%0A        %7D
+ serverDate = HttpHeaderParser.parseDateAsEpoch(headers.get(%22Date%22));%0A            %7D%0A%0A            final long
  ca
-t
 ch
- (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return Response.error(new ParseError());
+eHitButRefreshed = 30 * 60 * 1000; // in 30 minutes cache will be hit, but also refreshed on background%0A            final long cacheExpired = 30 * 60 * 1000; // in 30 minutes this cache entry expires completely%0A            final long softExpire = now + cacheHitButRefreshed;%0A            final long ttl = now + cacheExpired;%0A%0A            Cache.Entry entry = new Cache.Entry();%0A            entry.data = response.data;%0A            entry.softTtl = softExpire;%0A            entry.ttl = ttl;%0A            entry.serverDate = serverDate;%0A            entry.responseHeaders = headers;%0A%0A            return entry;%0A        %7D
 %0A   

change cache policy