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

后端 未结 1 795
时光取名叫无心
时光取名叫无心 2021-01-16 02:13

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 asyn

1条回答
  •  被撕碎了的回忆
    2021-01-16 02:50

    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 {
        /** 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);
    }
    

    0 讨论(0)
提交回复
热议问题