Created
October 14, 2015 05:09
-
-
Save shekhar12020/00c0e1966db3b676d712 to your computer and use it in GitHub Desktop.
Generic Object Request usnig Volley Library
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 CustomVolleyRequestQueue { | |
private static CustomVolleyRequestQueue mInstance; | |
private static Context mCtx; | |
private RequestQueue mRequestQueue; | |
private CustomVolleyRequestQueue(Context context) { | |
mCtx = context; | |
mRequestQueue = getRequestQueue(); | |
} | |
public static synchronized CustomVolleyRequestQueue getInstance(Context context) { | |
if (mInstance == null) { | |
mInstance = new CustomVolleyRequestQueue(context); | |
} | |
return mInstance; | |
} | |
public RequestQueue getRequestQueue() { | |
if (mRequestQueue == null) { | |
Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024); | |
Network network = new BasicNetwork(new HurlStack()); | |
mRequestQueue = new RequestQueue(cache, network); | |
// Don't forget to start the volley request queue | |
mRequestQueue.start(); | |
} | |
return mRequestQueue; | |
} | |
} |
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 GenericObjectRequest<R, T> extends Request<T> { | |
private final Gson gson = new Gson(); | |
private Response.Listener<T> listener; | |
private Class<R> inputType; | |
private Class<T> outputType; | |
private R object; | |
private static final String PROTOCOL_CHARSET = "utf-8"; | |
public GenericObjectRequest(int method, String url, R postObject, Class<R> inputType, Class<T> outputType, | |
Response.Listener<T> listener, | |
Response.ErrorListener errorListener) { | |
super(method, url, errorListener); | |
this.listener = listener; | |
this.inputType = inputType; | |
this.outputType = outputType; | |
this.object = postObject; | |
} | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
HashMap<String, String> headers = new HashMap<String, String>(); | |
//Optional headers | |
headers.put("Accept", ApplicationConstant.ACCEPT); | |
headers.put("Content-Type", ApplicationConstant.CONTENT_TYPE); | |
if (GlobalData.getInstance().getAuthToken()!=null){ | |
headers.put("Authorization", GlobalData.getInstance().getAuthToken()); | |
} | |
return headers; | |
} | |
@Override | |
public byte[] getBody() { | |
String jsonString = gson.toJson(object, inputType); | |
try { | |
return jsonString.getBytes(PROTOCOL_CHARSET); | |
} catch (UnsupportedEncodingException e) { | |
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", jsonString, PROTOCOL_CHARSET); | |
return null; | |
} | |
} | |
@Override | |
protected void deliverResponse(T response) { | |
listener.onResponse(response); | |
} | |
@Override | |
protected Response<T> parseNetworkResponse(NetworkResponse response) { | |
try { | |
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); | |
return Response.success(gson.fromJson(json, outputType), HttpHeaderParser.parseCacheHeaders(response)); | |
} catch (UnsupportedEncodingException e) { | |
return Response.error(new ParseError(e)); | |
} catch (JsonSyntaxException e) { | |
return Response.error(new ParseError(e)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Android Studio
include ':app', ':volley'
compile project(":volley")