Volley East, Fast Networking for Android
Android volley is a networking library, which was introduced to make networking calls easier, faster without writing a lot of codes.
There are many features in Volley:
- Automatic scheduling of network requests.
- Multiple concurrent network connections.
- Transparent disk and memory response caching with standard HTTP cache coherence.
- Cancelation request API.
It integrates easily with any protocol and comes out of the box with support for raw strings, images, and JSON.
So here, We will know how to return data from onResponse() of Volley to our function:
1. Make an interface:
1 2 3 |
public interface VolleyCallback { void onSuccessResponse(String result); } |
first of all, make an interface named VolleyCallback and interface method named onSuccessResponse.
2. How to use interface in our volley onResponse()
Here, I have made a function named getResponse(…, …, …, VolleyCallback callback) with VolleyCallback interface as a parameter, then
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public void getResponse(int method, String url, JSONObject jsonValue, final VolleyCallback callback) { queue = MySingleton.getInstance(mCtx).getRequestQueue(); StringRequest strreq = new StringRequest(Request.Method.GET, url, new Response.Listener < String > () { @Override public void onResponse(String Response) { callback.onSuccessResponse(Response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError e) { e.printStackTrace(); Toast.makeText(mCtx, e + "error", Toast.LENGTH_LONG).show(); } }) { // set headers @Override public Map < String, String > getHeaders() throws com.android.volley.AuthFailureError { Map < String, String > params = new HashMap < String, String > (); params.put("Authorization: Basic", TOKEN); return params; } }; MySingleton.getInstance(mCtx).addToRequestQueue(strreq); } |
In above code, getResponse() have 4 parameters:
1- int method – which define your method type which can be GET, POST, PUT, DELETE.
2- String url – this is your url, where you want to send a request and get a response.
3- JSONObject jsonValue – this is your JSON data which will be used when you send the post request.
4- VolleyCallback callback – This is your interface object.
and in onResponse(String Response) you can pass the Response value to the interface function onSuccessResponse(Response). Like
1 2 3 4 |
@Override public void onResponse(String Response) { callback.onSuccessResponse(Response); } |
If your application makes constant use of the network, it’s probably most efficient to set up a single instance of RequestQueue that will last the lifetime of your app. so the code
MySingleton.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
package com.example.amangupta.mobikul_cs_cart; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.Volley; import android.content.Context; import android.graphics.Bitmap; import android.support.v4.util.LruCache; import android.widget.Toast; public class MySingleton { private static MySingleton mInstance; private RequestQueue mRequestQueue; private ImageLoader mImageLoader; private static Context mCtx; private MySingleton(Context context) { mCtx = context; mRequestQueue = getRequestQueue(); mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() { private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20); @Override public Bitmap getBitmap(String url) { return cache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { cache.put(url, bitmap); } }); } public static synchronized MySingleton getInstance(Context context) { if (mInstance == null) { mInstance = new MySingleton(context); } return mInstance; } public RequestQueue getRequestQueue() { if (mRequestQueue == null) { // getApplicationContext() is key, it keeps you from leaking the // Activity or BroadcastReceiver if someone passes one in. mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); } return mRequestQueue; } public <T> void addToRequestQueue(Request<T> req) { getRequestQueue().add(req); } public ImageLoader getImageLoader() { return mImageLoader; } } |
3. How can you get the return data response in your function:
Here, I have made a function named myWebServiceFun() and call it whenever you want.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public void myWebServiceFun() { String url = "Your URL"; getResponse(Request.Method.GET, url, null, new GetVolleyResponse.VolleyCallback() { @Override public void onSuccessResponse(String result) { try { JSONObject response = new JSONObject(result); Snackbar.make(fragmentView, response.getString("message") + "", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); // do your work with response object } catch (JSONException e) { e.printStackTrace(); } } }); } |
In above code,
we have to override the function onSuccessResponse(). And you get a result as a response. Here, I have received a JSON data. And show the message which has sent from API function.