Skip to content

Instantly share code, notes, and snippets.

@svangsgaard
Last active April 22, 2022 01:53
Show Gist options
  • Select an option

  • Save svangsgaard/5858210 to your computer and use it in GitHub Desktop.

Select an option

Save svangsgaard/5858210 to your computer and use it in GitHub Desktop.
A helper class to Google volley. Made with inspiration from AFNetworking. Usage: volley = new VolleyHelper(this, "<BASEURL>"/*Maybe get url from getString*/); volley.post("<service_name>", null, new Listener<JSONObject>() { @OverRide public void onResponse(JSONObject response) { Log.i("Got JSON", response.toString()); } }, new ErrorListener() { @…
import org.json.JSONObject;
import android.content.Context;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
public class VolleyHelper {
private final Context mContext;
private final RequestQueue mRequestQueue;
private final ImageLoader mImageLoader;
private final String mBaseUrl;
public VolleyHelper(Context c, String baseUrl){
mContext = c;
mRequestQueue = Volley.newRequestQueue(mContext);
mBaseUrl = baseUrl;
mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache());
}
private String contructUrl(String method){
return mBaseUrl + "/" + method;
}
public ImageLoader getImageLoader(){
return mImageLoader;
}
public void get(String method, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener){
JsonObjectRequest objRequest = new JsonObjectRequest(Method.GET, contructUrl(method), jsonRequest, listener, errorListener);
mRequestQueue.add(objRequest);
}
public void put(String method, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener){
JsonObjectRequest objRequest = new JsonObjectRequest(Method.PUT, contructUrl(method), jsonRequest, listener, errorListener);
mRequestQueue.add(objRequest);
}
public void post(String method, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener){
JsonObjectRequest objRequest = new JsonObjectRequest(Method.POST, contructUrl(method), jsonRequest, listener, errorListener);
mRequestQueue.add(objRequest);
}
public void delete(String method, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener){
JsonObjectRequest objRequest = new JsonObjectRequest(Method.DELETE, contructUrl(method), jsonRequest, listener, errorListener);
mRequestQueue.add(objRequest);
}
}
@zxfjd3g

zxfjd3g commented Jun 25, 2013

Copy link
Copy Markdown

w

@AndrewHaisting

Copy link
Copy Markdown

Could you show your implementation of BitmapLruCache?

Thank you

@svangsgaard

Copy link
Copy Markdown
Author

Sure. I will add a new gist.

Edit:
Here's the BitmapLruCache: https://gist.github.com/svangsgaard/5866044

@abhinav-adtechs

Copy link
Copy Markdown

Thank you so much! Much needed!

@zarulizham

zarulizham commented Mar 30, 2017

Copy link
Copy Markdown

What is <service_name> actually?

Edited:
Nevermind. Just find out what is it. Nice helper class BTW :)

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