Skip to content

Instantly share code, notes, and snippets.

@shekhar12020
Created October 14, 2015 05:09
Show Gist options
  • Save shekhar12020/00c0e1966db3b676d712 to your computer and use it in GitHub Desktop.
Save shekhar12020/00c0e1966db3b676d712 to your computer and use it in GitHub Desktop.
Generic Object Request usnig Volley Library
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;
}
}
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));
}
}
}
@shekhar12020
Copy link
Author

Android Studio

  1. First get latest volley with git (git clone https://android.googlesource.com/platform/frameworks/volley).
  2. In your current project (android studio) click [file] --> [Import Module].
  3. Now select the directory where you downloaded Volley to.
  4. Now Android studio might guide you to do the rest but continue guide to verify that everything works correct
  5. Open settings.gradle (find in root) and add (or verify this is included):
    include ':app', ':volley'
  6. Now go to your build.gradle in your project and add the dependency:
    compile project(":volley")
  7. Gradle sync, and you're done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment