Last active
April 22, 2022 01:53
-
-
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() { @…
This file contains hidden or 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
| 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); | |
| } | |
| } |
Could you show your implementation of BitmapLruCache?
Thank you
Author
Sure. I will add a new gist.
Edit:
Here's the BitmapLruCache: https://gist.github.com/svangsgaard/5866044
Thank you so much! Much needed!
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
w