i try to use cache request response from Fast Android Networking for 'POST' And 'GET' Request
But I cant find any method to handle it!
Is it possible to read response from cache?
how i can handle it?
Does it works when device is offline ?
I have provided some examples for that, please refer
https://amitshekhariitbhu.github.io/Fast-Android-Networking/caching.html
Thanks
Amit Shekhar
thank you
so i want to know it caches locally and able to response from cache at offline mode ?
I Just Defined Two Methods to set offline cache
public void saveResponse(Context context, String content, String name) {
Calendar calendar = Calendar.getInstance();
final int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
Log.i("SAVE_TAG", "saveResponse: "+DAY_OF_YEAR);
name = name+"-"+DAY_OF_YEAR;
SharedPreferences shp = context.getSharedPreferences(.shp_app, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shp.edit();
editor.putString(name, content);
editor.apply();
}
public void getSavedResponse(Context context, String name, JSONObjectRequestListener jsonObjectRequestListener) {
if(jsonObjectRequestListener == null) {
return;
}
Calendar calendar = Calendar.getInstance();
final int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
Log.i("SAVE_TAG", "getSavedResponse: " + DAY_OF_YEAR);
name = name + "-" + DAY_OF_YEAR;
Log.i("SAVE_TAG", "getSavedResponse: " + name);
SharedPreferences shp = context.getSharedPreferences(shp_app, Context.MODE_PRIVATE);
String content = shp.getString(name, "");
Log.i("SAVE_TAG", "getSavedResponse: " + content);
if(content.isEmpty()) {
jsonObjectRequestListener.onError(new ANError("Response Is Empty At Offline Mode!"));
}
try {
JSONObject jsonObject = new JSONObject(content);
jsonObjectRequestListener.onResponse(jsonObject);
} catch (JSONException je) {
jsonObjectRequestListener.onError(new ANError(je.getMessage()));
}
}
And Usage
No Internet Connection
getSavedResponse(this, TAG_MAIN, jsonObjectRequestListener); // To Get Saved Response
On
onResponseAtjsonObjectRequestListener
saveResponse(getApplicationContext(), response.toString(), TAG_MAIN); // To Save Response
If the internet is not present you have to add getResponseOnlyIfCached().
Most Developers Like to Use Single Code inside Method With Parameters, I suggest to put Enum to handle getResponseOnlyIfCached() And doNotCacheResponse() And getResponseOnlyFromNetwork()
thank You for your Answer
Your suggestion is welcome.
As of now, you can do so:
ANRequest.GetRequestBuilder getRequestBuilder = new ANRequest.GetRequestBuilder("YourUrl");
if(interentNotConnected){
getRequestBuilder.getResponseOnlyIfCached();
}
getRequestBuilder
.build()
.getAsJSONArray(listener)
are Fast Android Networking processing Last Modified Cache from response server? or only max-age?
i store json mysql query result in file cache in my server, my trouble is when i update file cache on server , method is set Last Modified on response header.
i want to FAN request to my server and cached it localy, and sync when server cache is update.
i need this method because my server cache used by everyone, i dont get better sync with max-age, because it must be wait until max-age exipred
i wish somebody can help.thanks!
@mindfreak269 Yes, it processes Last-Modified-Cache from the server response.
@amitshekhariitbhu : how to implement that? are must using getResponseOnlyFromNetwork() ? because if i dont use getResponseOnlyIfCached() And doNotCacheResponse() And getResponseOnlyFromNetwork()
it will be prority from local cache .
It automatically does this. If your server sends Cache-Control Tag. When it makes the request to the server, it first verifies the already cached data, if server returns 304, it gives from cache.
While using ANImageview in RecyclerView or ListView, loaded image cache not store, is there any method provided that functionality?
@hitesh-dhamshaniya As of now for ANImageview, disabling cache is not present
So, it's not advisable to use it with RecyclerView and ListView, right?
@hitesh-dhamshaniya : While loading image in RecyclerView, this library is similar to volley, but I should say Glide and fresco are the better option for smooth scrolling.
if using recycleview,for best using image request, i do that, and after done save it to variable Bitmap in Holder, set if Bitmap is null, do request, if exist set imageView from bitmap, it can make recycleview and Fast Android Networking smoth, Don't do request every item recycleview attached
@mindfreak269 : if you use ANImageview in recycleview, it automatically download only first time, and put in memcache, then from next time it gets from cache.
@amitshekhariitbhu oh yes, i forget, i do that for disabling cache, and use getResponseOnlyFromNetwork(); and store it in variable, so no cache saved in local, due lot of image and internal sotrage limit.
Thanks for the examples @amitshekhariitbhu but those are all exceptions. Can you provide the most simple example, that just respects the cache control?
I've set my json file on the server with header("Cache-Control: max-age=3600โ). Is that enough?
My AnalyticsListener is always showing isFromCache 'false' on a very simple AndroidNetworking.get request with no special parameters, repeated one after another with 2 second delay.
@manuelpaulo one thing can be, I am just guessing, have you initialize the library with the context as the context is needed to create the cache.
@amitshekhariitbhu Bingo! Working now!
AndroidNetworking.initialize(getApplicationContext());
You should add that to the caching docs page!
Thanks!
please help how to working caching?
Please tell us what exact problem are you facing? Have you checked the above comment?
I'm first time working caching.so how to use library?
`AndroidNetworking.post(carUrl)
.setPriority(Priority.HIGH)
.getResponseOnlyFromNetwork()
.setMaxAgeCacheControl(0, TimeUnit.SECONDS)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
parseResponse(response);
Log.e(TAG, "onResponse: "+response );
}
@Override
public void onError(ANError anError) {
Log.e(TAG, "onError: "+anError.getErrorDetail() );
}
});`
Caches are always null @amitshekhariitbhu Can you please check.
/System.out: cache: null
2019-07-16 15:07:36.280 24115-24115/com.apporio.demotaxiapp I/APIExecution: isFromCache : false
2019-07-16 15:07:37.921 24115-24115/com.apporio.demotaxiapp I/APIExecution: isFromCache : false
2019-07-16 15:07:38.151 24115-24602/com.apporio.demotaxiapp I/System.out: cache: null
2019-07-16 15:07:38.164 24115-24115/com.apporio.demotaxiapp I/APIExecution: isFromCache : false
2019-07-16 15:07:38.184 24115-24600/com.apporio.demotaxiapp I/System.out: cache: null
2019-07-16 15:07:38.204 24115-24115/com.apporio.demotaxiapp I/APIExecution: isFromCache : false
2019-07-16 15:07:39.037 24115-24610/com.apporio.demotaxiapp I/System.out: cache: null
2019-07-16 15:07:39.048 24115-24115/com.apporio.demotaxiapp I/APIExecution: isFromCache : false
2019-07-16 15:07:42.194 24115-24115/com.apporio.demotaxiapp I/APIExecution: isFromCache : false
Hello @amitshekhariitbhu
I tried following for get response while internet off :
ANRequest.GetRequestBuilder getRequestBuilder = new ANRequest.GetRequestBuilder(url);
if (!Util.isNetworkAvailable(TestActivity.this)) {
getRequestBuilder.getResponseOnlyIfCached();
status = "Offline";
} else {
getRequestBuilder.setMaxAgeCacheControl(3600, TimeUnit.SECONDS);
getRequestBuilder.setMaxStaleCacheControl(365, TimeUnit.SECONDS);
status = "Online";
}
getRequestBuilder
.addHeaders("Authorization", Constants.AUTOTHORIZATION_HEADER_VALUE)
.build()
.getAsString(new StringRequestListener() {
@Override
public void onResponse(String response) {
tvVersion.setText(status + "\n" + response);
}
@Override
public void onError(ANError error) {
error.printStackTrace();
tvVersion.setText(error.getErrorCode()+" : " + error.getErrorDetail() + " : "
+ error.getResponse() + " : " + error.getErrorBody());
}
});
But, internet is off, i am getting following response :
Error Code : 504
responseFromServerError
message=Unsatisfiable Request ( only if cached)
please can you help me on it.
Most helpful comment
@hitesh-dhamshaniya : While loading image in RecyclerView, this library is similar to volley, but I should say Glide and fresco are the better option for smooth scrolling.