Open croccio opened 10 years ago
@croccio can you paste the code in the issue? thanks.
@raulraja done
@croccio That type of cache policy only works if you have previously stored the response from a connected call otherwise it would return null. I recommend you use CachePolicy.ENABLED instead if you just want to load cached results at all times. The first time it will hit the network but subsequent calls would load from the cache. For more info on the cache policies take a look at https://github.com/47deg/appsly-android-rest#cache
@raulraja I've tried all policy and if 'm offline it return nuill. if i put this in on response method i get my cached object
try { Pair<Object, Boolean> cachedResult = new Pair<Object, Boolean>(getCacheManager().get(response.getCacheInfo().getKey(), response.getCacheInfo()), false); if (cachedResult.first != null) { Log.e("PAIR OBJECT", ((MioJson) cachedResult.first).getNome() + ""); }else{ Log.e("PAIR OBJECT", "Cache expired"); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }
You are not supposed to use the CacheManager
directly. Use response.getCacheInfo().isLoadedFromCache()
if i call that method it return true, but if i close app, turn off internet, open app, on response method return me null object. it return null object also if i'm offline without closing the app
@croccio @raulraja i have the same problem, after first opening app and first call, i save a value in cache for a certain path, than if i remove internet connection onResume method gave me NULL, i'm pretty sure that cache shouldn't gave me null value... is there a problem of library ?
Maybe some issue here https://github.com/47deg/appsly-android-rest/blob/master/android-rest/src/main/java/ly/apps/android/rest/cache/CacheAwareHttpClient.java#L123 but can't really tell if it's something specific to some devices or the Connectivity Manager. Turning off internet does not mean not being connected to a network. Do you guys see anything strange in the logcat?
here you are logcat. first while cache is valid, second after cache is INVALID
07-31 15:28:10.063 6948-6948/com.antonio.restrung D/android-rest﹕ main -> invoking: http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json with body: null and temporary request content type: application/json 07-31 15:28:10.063 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest 07-31 15:28:10.079 6948-6965/com.antonio.restrung D/android-rest﹕ AsyncTask #1 -> image read from disk 7a1b4733e23d18f6dfc39974cfb2971 07-31 15:28:10.079 6948-6965/com.antonio.restrung D/android-rest﹕ AsyncTask #1 -> Loading from cache because expiresOn : 1406813297361 > now : 1406813290088 07-31 15:28:10.094 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest.onPostExecute proceeding with cache: android.util.Pair@4215eb7d 07-31 15:28:10.094 6948-6948/com.antonio.restrung D/android-rest﹕ main -> onSuccess: status200 rawResponse: null 07-31 15:28:10.094 6948-6948/com.antonio.restrung E/RESPONSE﹕ ly.apps.android.rest.client.Response@42160648 07-31 15:28:10.102 6948-6948/com.antonio.restrung D/android-rest﹕ main -> image read from disk 7a1b4733e23d18f6dfc39974cfb2971 07-31 15:28:10.102 6948-6948/com.antonio.restrung D/android-rest﹕ main -> Loading from cache because expiresOn : 1406813297361 > now : 1406813290111 07-31 15:28:10.102 6948-6948/com.antonio.restrung E/PAIR OBJECT﹕ ciao antonio 07-31 15:28:26.430 6948-6948/com.antonio.restrung D/android-rest﹕ main -> invoking: http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json with body: null and temporary request content type: application/json 07-31 15:28:26.430 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest 07-31 15:28:26.454 6948-6971/com.antonio.restrung D/android-rest﹕ AsyncTask #3 -> image read from disk 7a1b4733e23d18f6dfc39974cfb2971 07-31 15:28:26.454 6948-6971/com.antonio.restrung D/android-rest﹕ AsyncTask #3 -> Invalidated key : http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json from cache because expiresOn : 1406813297361 < now : 1406813306463 07-31 15:28:26.454 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest.onPostExecute proceeding uncached 07-31 15:28:26.485 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareCallback. Loading from cache after response error: UnknownHostException exception: Unable to resolve host "alessioarsuffipaybay.altervista.org": No address associated with hostname result with cached result: null 07-31 15:28:26.485 6948-6948/com.antonio.restrung D/android-rest﹕ main -> onFailure: status0 rawResponse: null 07-31 15:28:26.485 6948-6948/com.antonio.restrung E/RESPONSE﹕ ly.apps.android.rest.client.Response@420fd0a8 07-31 15:28:26.485 6948-6948/com.antonio.restrung D/android-rest﹕ main -> image read from disk 7a1b4733e23d18f6dfc39974cfb2971 07-31 15:28:26.485 6948-6948/com.antonio.restrung D/android-rest﹕ main -> Invalidated key : http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json from cache because expiresOn : 1406813297361 < now : 1406813306495 07-31 15:28:26.485 6948-6948/com.antonio.restrung E/PAIR OBJECT﹕ Cache expired
it cannot connect to url to get json and so it doesn't return cached object.
I don't think that is the reason. It invalidated the cache previous to the returned value becase your second request was 9 seconds after your timetolive.
Invalidated key : http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json
from cache because expiresOn : 1406813297361 < now : 1406813306463
ok, but if the cache is valid it return me null
here it return me null
07-31 15:28:10.063 6948-6948/com.antonio.restrung D/android-rest﹕ main -> invoking: http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json with body: null and temporary request content type: application/json 07-31 15:28:10.063 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest 07-31 15:28:10.079 6948-6965/com.antonio.restrung D/android-rest﹕ AsyncTask #1 -> image read from disk 7a1b4733e23d18f6dfc39974cfb2971 07-31 15:28:10.079 6948-6965/com.antonio.restrung D/android-rest﹕ AsyncTask #1 -> Loading from cache because expiresOn : 1406813297361 > now : 1406813290088 07-31 15:28:10.094 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest.onPostExecute proceeding with cache: android.util.Pair@4215eb7 07-31 15:28:10.094 6948-6948/com.antonio.restrung D/android-rest﹕ main -> onSuccess: status200 rawResponse: null 07-31 15:28:10.094 6948-6948/com.antonio.restrung E/RESPONSE﹕ ly.apps.android.rest.client.Response@4216064
and now cache isn't invalidate because:
Loading from cache because expiresOn : 1406813297361 > now : 1406813290088
Do you see the same behavior with CachePolicy.ENABLED
?
yer. i've same result with all policy
I'm not sure what the issue is. It appears as if the cache had no effect in your terminal. Can you try with a different device?
i've the problem with galaxy nexus(pa android 4.4), nexus 4(stock android 4.4) and lg optimus one (p500with android 2.3.7 cm)
if you would i can try on s3 and s2 with stock firmaware
can you tell me how can i get cached object here? maybe i'm doing something wrong
api.getMyJson(new Callback
@Override
public void onResponse(Response<MyJson> myJsonResponse) {
//How get cached object
}
});
You don't need to do anything there myJsonResponse.result
is the cached object. If you want to know if it was loaded from the cache or not use myJsonResponse.getCacheInfo().isLoadedFromCache()
Ok. If i do so, it give me null on 5 device. Maybe there is a problem in library. I'll try to see it.
Hello! I'm using your android library to do rest request and to use cache. How can i get cached result? If i use annotation than when i call api.myrequest it give me null and not cached object. thank u.
package com.antonio.restrung;
import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button;
import org.apache.http.Header;
import java.io.IOException;
import ly.apps.android.rest.cache.CacheManager; import ly.apps.android.rest.cache.ContextPersistentCacheManager; import ly.apps.android.rest.client.Callback; import ly.apps.android.rest.client.Response; import ly.apps.android.rest.client.RestClient; import ly.apps.android.rest.client.RestClientFactory; import ly.apps.android.rest.client.RestServiceFactory;
public class MyActivity extends ActionBarActivity {
}
___-
package com.antonio.restrung;
import java.io.Serializable;
import ly.apps.android.rest.client.RestClient; import ly.apps.android.rest.client.RestClientFactory; import ly.apps.android.rest.client.RestServiceFactory;
/**
Created by antonio on 31/07/14. */ public class MioJson implements Serializable{
String nome; String cognome;
public String getNome() { return nome; }
public void setNome(String nome) { this.nome = nome; }
public String getCognome() { return cognome; }
public void setCognome(String cognome) { this.cognome = cognome; }
}
package com.antonio.restrung;
import ly.apps.android.rest.cache.CachePolicy; import ly.apps.android.rest.client.Callback; import ly.apps.android.rest.client.annotations.Cached; import ly.apps.android.rest.client.annotations.GET; import ly.apps.android.rest.client.annotations.QueryParam; import ly.apps.android.rest.client.annotations.RestService;
/**
Created by antonio on 31/07/14. */ @RestService public interface GetJson {
@GET("/alessioarsuffipaybay_0001.json") @Cached(policy = CachePolicy.LOAD_IF_OFFLINE, timeToLive = 60 * 1000) public void getMioJson(Callback callback);
}