Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley library: do we always have to repeat Response.Listener and Response.ErrorListener

I have recently started using Android Volley in my project. The common practice mentioned in most of the tutorials is to use it this way:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
            url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    // do something
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // do something.
                }
            });

My query is - Do we have to repeat this code every where wherever we need to make a request. I was trying to put in a common onResponse and onErrorResponse handler by creating a helper class :

public class CustomJSONObjectRequest extends JsonObjectRequest {
    private BaseResource resource;
    private String queryId;
    private String paramArgs;

    public CustomJSONObjectRequest(int method, String url, JSONObject 
                               jsonRequest, 
                               Response.Listener<JSONObject> listener,
                               final Response.ErrorListener errorListener, 
                               final Context ctx,
                               final BaseResource baseResource) {
    super(method, url, jsonRequest,
            new Response.Listener<JSONObject>() {
                 // some common code for all BaseResources
            },
            new Response.ErrorListener() {
                // some common code
            });
}

But the problem with this approach is that I need to pass in each and every thing in the constructor itself, which is making me feel like I am not doing it correctly. For example, if i need to pass some query parameters for the url, i need to always pass in the complete url from the caller although I can still have a common logic to generate the url in one place.

Can someone please let me know what is the best way of achieving something like this.

like image 299
pankaj Avatar asked Sep 19 '15 20:09

pankaj


1 Answers

You can refer to my sample code as the following:

public interface VolleyResponseListener {
    void onError(String message);

    void onResponse(Object response);
}

Then in my VolleyUtils class:

public static void makeJsonObjectRequest(Context context, String url, final VolleyResponseListener listener) {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (url, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    listener.onResponse(response);
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    listener.onError(error.toString());
                }
            }) {

        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            try {
                String jsonString = new String(response.data,
                        HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
                return Response.success(new JSONObject(jsonString),
                        HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }
        }
    };

    // Access the RequestQueue through singleton class.
    VolleySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest);
}

Then in Activity:

VolleyUtils.makeJsonObjectRequest(mContext, url, new VolleyResponseListener() {
        @Override
        public void onError(String message) {

        }

        @Override
        public void onResponse(Object response) {

        }
    });

Another way is creating a VolleyResponseListener variable then passing it into methods of VolleyUtils class, as my answer in the following question:

Android: How to return async JSONObject from method using Volley?

Hope this helps!

like image 70
BNK Avatar answered Oct 20 '22 23:10

BNK