How to Set Tag to the request and get it from Response Volley asynchronous request?
Asked Answered
J

1

1

I have an Android application with multiple REST Api's. The API's are managed using the Volley library. The response is getting and it's woking fine. But when I make asynchronous requests, I can't identify the response of each request.

My request method is this:

private void httpCall(String URL, String json, String session key, int type) {
        try {
            SSLContext sslcontext = SSLContext.getInstance("TLSv1");
           sslcontext.init(null,
                    null,
                    null);
            SSLSocketFactory NoSSLv3Factory = new NoSSLv3SocketFactory(sslcontext.getSocketFactory());
            HttpsURLConnection.setDefaultSSLSocketFactory(NoSSLv3Factory);

            Log.i(REQUEST_TAG, "httpCall=url" + url + "::type" + type);
            Log.i(REQUEST_TAG, "httpCall=json" + json);
        } catch (Exception e) {
            e.printStackTrace();

        }
        if (mContext != null)
            mQueue = CustomVolleyRequestQueue.getInstance(mContext).getRequestQueue();
        else
            mQueue = CustomVolleyRequestQueue.getInstance(mActivity).getRequestQueue();
        JSONObject mJSONObject;
        final CustomJSONObjectRequest jsonRequest;
        try {
            if ((json != null) && (json.trim().length() > 0)) {
                mJSONObject = new JSONObject(json);
            } else {
                mJSONObject = new JSONObject();
            }
            jsonRequest = new CustomJSONObjectRequest(sessionkey, type, url, mJSONObject, this, this);
            // Wait 20 seconds and don't retry more than once
            jsonRequest.setRetryPolicy(new DefaultRetryPolicy(
                    (int) TimeUnit.SECONDS.toMillis(20),
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

            jsonRequest.setTag(REQUEST_TAG);
            mQueue.add(jsonRequest);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

Is there any option to set a tag to the request and get the same from the response?. So that I can identify the current request and response. I don't know this is a duplicate question, but I didn't get a proper explanation for this.

My Response method is:

@Override
    public void onResponse(Object response) {
        if (response != null) {

            // I want to trigger the request tag from here

            mCallBack.onSuccessData(response);
        }
    }

The request and response method are in same class and the class implemented Response.Listener, Response.ErrorListener.

Janellejanene answered 21/3, 2016 at 9:59 Comment(6)
I'm not sure what you are trying to use tags for, but where do you implement the onResponse method?Paryavi
I modify my question. The request and response methods are in same class and I want to set a tag/key in the request and trigger the same on the response.Janellejanene
trigger the same means?? Tags in volley are used to refer the request object. The request has already been completed on the response method. what do you wana do with it now?Shrill
Please check the scenario: I am passing multiple requests a time. The requests are different Api's. Then how can I segregate each request and response? Or Is any alternative method for that? I am using this as a generalised class in my application. So all request are handled from here.Janellejanene
You only can add one request to the request queue at once, so you aren't passing multiple at once. You care about the data from the callback, which is asynchronous. If you need the result of one request to do another, then you create your second request in the onResponse of the firstParyavi
Yes, I found a solution for that. I set a unique key for identifying the response type in each API response from the server. So that I can asynchronously call the request and collect the response based on the key. Thank for your support.Janellejanene
C
2

The tag you assign to a Request is stored to the variable mTag which is persisted throughout the life cycle of the Request.

public Request<?> setTag(Object tag) {
    mTag = tag;
    return this;
}

For my applications I have slightly modified the following Volley classes:

In class Request change visibility of mTag from private to protected

/** An opaque token tagging this request; used for bulk cancellation. */
    protected Object mTag;

In class Response, add Object tag to the callback function onResponse defined in interface Listener

/** Callback interface for delivering parsed responses. */
public interface Listener<T> {
    /** Called when a response is received. */
    public void onResponse(Object tag, T response);
}

In classes which implement interface Response.Listener, like JsonRequest

@Override
protected void deliverResponse(T response) {
    mListener.onResponse(mTag, response);
}
Corneille answered 10/5, 2016 at 5:10 Comment(5)
The same procedure can be adopted for error response by modifying interface ErrorListener method as per onErrorResponse(ObjectmTag, VolleyError error)Corneille
Do you have any example for the same.Tommi
@Corneille what if you have included it as a gradle dependency and not as a module. Is there a work around tooBract
@Sagar Devanga you can create extend and implement custom Request and Response classes to add tagging.Corneille
@Corneille amazing. I forgot that route totally. thanx a lot.Bract

© 2022 - 2024 — McMap. All rights reserved.