1
votes

i'am not an expert handling with http but what is was trying to accomplish is:

If the response is in cache return cache if not return network response simple as that.

But the problem is the the response code is always 504 but i'am sure that it's cached, so for my understanding it should return != 504, the code is in the "doGetRequest" method.

My okhttp client:

public class RestAsyncHttpClient {

/* Constants */
private static final String TAG = "RestAsyncHttpClient";
private static long HTTP_CACHE_SIZE = 10 * 1024 * 1024; // 10 MiB
private static final String DISK_CACHE_SUBDIR = "cacheApi";

/* Properties */
private static OkHttpClient mHttpClient;
private static Cache cache;
private static RestAsyncHttpClient instance = null;
private Context context;

public static RestAsyncHttpClient getInstance() {
    if (instance == null) {
        instance = new RestAsyncHttpClient();
    }
    return instance;
}

/**
 * Initialize HttpClient
 */
public void initialize(Context context) {
    setContext(context);

    mHttpClient = new OkHttpClient();

    configureSslSocketFactory();

    configureCache();

    configureTimeouts();
}

private void setContext(Context context) {
    this.context = context;
}

private void configureSslSocketFactory() {
    mHttpClient.setSslSocketFactory(HttpsURLConnection.getDefaultSSLSocketFactory());
}

public static void doGetRequest(String url, Callback callback) throws IOException {
    Request cachedRequest = new Request.Builder()
            .cacheControl(new CacheControl.Builder().onlyIfCached().build())
            .url(url)
            .build();
    Response forceCacheResponse = mHttpClient.newCall(cachedRequest).execute();
    if (forceCacheResponse.code() != 504) {
        // The resource was cached! Show it.
        callback.onResponse(forceCacheResponse);

    } else {
        Request networkRequest = new Request.Builder().url(url).build();
        mHttpClient.newCall(networkRequest).enqueue(callback);
    }
}

private void configureCache() {
    if (cache == null)
        cache = createHttpClientCache(context);

    mHttpClient.setCache(cache);
}

private static Cache createHttpClientCache(Context context) {
    try {
        File cacheDir = context.getDir("cache_api", Context.MODE_PRIVATE);
        return new Cache(cacheDir, HTTP_CACHE_SIZE);

    } catch (IOException exp) {
        LogHelper.error(TAG, "Couldn't create http cache because of IO problem.", exp);
        return null;
    }
}

private void configureTimeouts() {
    mHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
    mHttpClient.setWriteTimeout(35, TimeUnit.SECONDS);
    mHttpClient.setReadTimeout(35, TimeUnit.SECONDS);
}

}

My cache is being filled so it should read from cache when no connection available. cache files

Response Headers from server:

  • Connection:close
  • Content-Type:application/json; charset=iso-8859-1
  • Date:Mon, 23 Mar 2015 11:04:44 GMT
  • Server:Apache
  • Transfer-Encoding:chunked
  • X-Powered-By:Servlet/2.5 JSP/2.1

Request Headers:

  • Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
  • Accept-Encoding:gzip, deflate, sdch
  • Accept-Language:pt-PT,pt;q=0.8,en-US;q=0.6,en;q=0.4,fr;q=0.2
  • Cache-Control:max-age=0
  • Connection:keep-alive
  • Host:cantShare:448
  • User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36
1

1 Answers

1
votes

When you get a response back, you need to read it entirely, otherwise it won't be cached. This is because OkHttp only commits the response to the cache when you finish reading its body.

You don't need the special case for 504. Just make the request regularly, and it'll use the cache if it can.