如何将标签设置为请求并从Response Volley异步请求中获取它? [英] How to Set Tag to the request and get it from Response Volley asynchronous request?
问题描述
我有一个具有多个REST Api的Android应用程序。使用Volley库管理API。响应越来越好了。但是当我发出异步请求时,我无法识别每个请求的响应。
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.
我的请求方法是:
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.
我的响应方法是:
@Override
public void onResponse(Object response) {
if (response != null) {
// I want to trigger the request tag from here
mCallBack.onSuccessData(response);
}
}
请求和响应方法在同一类中,并且已实现的类Response.Listener,Response.ErrorListener。
The request and response method are in same class and the class implemented Response.Listener, Response.ErrorListener.
推荐答案
您分配给请求的标签存储在变量中mTag (在请求的整个生命周期中保持不变)。
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;
}
对于我的应用程序,我对以下Volley类进行了一些修改:
For my applications I have slightly modified the following Volley classes:
在类请求中,将mTag的可见性从私有更改为受保护
In class Request change visibility of mTag from private to protected
/** An opaque token tagging this request; used for bulk cancellation. */
protected Object mTag;
在类响应中,添加对象标签接口 Listener
/** Callback interface for delivering parsed responses. */
public interface Listener<T> {
/** Called when a response is received. */
public void onResponse(Object tag, T response);
}
在实现接口 Response.Listener 的类中,像 JsonRequest
In classes which implement interface Response.Listener, like JsonRequest
@Override
protected void deliverResponse(T response) {
mListener.onResponse(mTag, response);
}
这篇关于如何将标签设置为请求并从Response Volley异步请求中获取它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!