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

你离开我真会死。 提交于 2019-12-01 12:34:25

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!