Last active
April 18, 2019 10:28
-
-
Save akirasosa/6697643 to your computer and use it in GitHub Desktop.
Volley. Use cache if there is no internet connection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyStringRequest extends StringRequest{ | |
public MyStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) { | |
super(method, url, listener, errorListener); | |
} | |
@Override | |
public void deliverError(VolleyError error) { | |
if (error instanceof NoConnectionError) { | |
Cache.Entry entry = this.getCacheEntry(); | |
if(entry != null) { | |
Response<String> response = parseNetworkResponse(new NetworkResponse(entry.data, entry.responseHeaders)); | |
deliverResponse(response.result); | |
return; | |
} | |
} | |
super.deliverError(error); | |
} | |
} |
Thanq so much..
Without deprecated methods:
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import java.net.HttpURLConnection;
public class MyStringRequest extends StringRequest {
public MyStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
}
@Override
public void deliverError(VolleyError error) {
if (error instanceof NoConnectionError) {
Cache.Entry entry = this.getCacheEntry();
if(entry != null) {
Response<String> response = parseNetworkResponse(new NetworkResponse(HttpURLConnection.HTTP_OK,
entry.data, false, 0, entry.allResponseHeaders));
deliverResponse(response.result);
return;
}
}
super.deliverError(error);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much.