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.

Nithinjith
  • 1,775
  • 18
  • 40
  • I'm not sure what you are trying to use tags for, but where do you implement the onResponse method? – OneCricketeer Mar 22 '16 at 06:55
  • 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. – Nithinjith Mar 22 '16 at 07:06
  • 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? – Mansha Chuttani Mar 22 '16 at 09:35
  • 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. – Nithinjith Mar 22 '16 at 11:12
  • 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 first – OneCricketeer Mar 22 '16 at 11:34
  • 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. – Nithinjith May 10 '16 at 05:26

1 Answers1

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);
}
Chebyr
  • 2,171
  • 1
  • 20
  • 30