How to Set Tag to the request and get it from Resp

2019-02-28 23:19发布

问题:

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.

回答1:

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);
}