i use volley to receive data from local server with php and mysql database. my question is how use cache when user offline?
my CustomRequest is this:
public class CustomRequest extends Request<JSONArray> {
private Listener<JSONArray> listener;
private Map<String, String> params;
public CustomRequest(int method, String url, Map<String, String> params,
Listener<JSONArray> reponseListener, ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return params;
}
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
protected void deliverResponse(JSONArray response) {
listener.onResponse(response);
}
}
and NetworkConnection is this:
public class NetworkConnection {
private static NetworkConnection instance;
private Context mContext;
private RequestQueue mRequestQueue;
public NetworkConnection(Context c){
mContext = c;
mRequestQueue = getRequestQueue();
}
public static NetworkConnection getInstance( Context c ){
if( instance == null ){
instance = new NetworkConnection( c.getApplicationContext() );
}
return( instance );
}
public RequestQueue getRequestQueue(){
if( mRequestQueue == null ){
mRequestQueue = Volley.newRequestQueue(mContext);
}
return(mRequestQueue);
}
public <T> void addRequestQueue( Request<T> request ){
getRequestQueue().add(request);
}
public void execute( final Transaction transaction, final String tag ){
WrapObjToNetwork obj = transaction.doBefore();
Gson gson = new Gson();
if( obj == null ){
return;
}
HashMap<String, String> params = new HashMap<>();
params.put("jsonObject", gson.toJson(obj));
CustomRequest request = new CustomRequest(Request.Method.POST,
"http://182:168:1:1:8282/new/package/ctrl/CtrlNew.php",
params,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Log.i("LOG", tag+" ---> "+response);
transaction.doAfter(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("LOG", "onErrorResponse(): "+error.getMessage());
transaction.doAfter(null);
}
});
request.setTag(tag);
request.setRetryPolicy(new DefaultRetryPolicy(5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
addRequestQueue(request);
}
}
and this code in fargment
// NETWORK
@Override
public WrapObjToNetwork doBefore() {
mPbLoad.setVisibility( (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) ? View.GONE : View.VISIBLE );
if( UtilTCM.verifyConnection(getActivity()) ){
Car car = new Car();
car.setCategory(0);
if( mList != null && mList.size() > 0 ){
car.setId(mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing() ? mList.get(0).getId() : mList.get(mList.size() - 1).getId());
}
return( new WrapObjToNetwork(car, "get-cars", (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) ) );
}
return null;
}
@Override
public void doAfter(JSONArray jsonArray) {
mPbLoad.setVisibility(View.GONE );
if( jsonArray != null ){
CarAdapter adapter = (CarAdapter) mRecyclerView.getAdapter();
Gson gson = new Gson();
int auxPosition = 0, position;
if( mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing() ){
mSwipeRefreshLayout.setRefreshing(false);
auxPosition = 1;
}
try{
for(int i = 0, tamI = jsonArray.length(); i < tamI; i++){
Car car = gson.fromJson( jsonArray.getJSONObject( i ).toString(), Car.class );
position = auxPosition == 0 ? mList.size() : 0;
adapter.addListItem(car, position);
if( auxPosition == 1 ){
mRecyclerView.getLayoutManager().smoothScrollToPosition(mRecyclerView, null, position);
}
}
if( jsonArray.length() == 0 && auxPosition == 0 ){
isLastItem = true;
}
}
catch(JSONException e){
Log.i(TAG, "doAfter(): "+e.getMessage());
}
}
else{
Toast.makeText(getActivity(), "Falhou. Tente novamente.", Toast.LENGTH_SHORT).show();
}
}
please help. thanks