我有一个带有多个REST Api的Android应用程序.使用Volley库管理API.响应越来越好了.但是当我发出异步请求时,我无法识别每个请求的响应.
我的请求方法是这样的:
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();
}
}
是否可以为请求设置标签并从响应中获取标签?这样我就可以识别当前的请求和响应.我不知道这是一个重复的问题,但是我没有得到适当的解释.
我的响应方法是:
@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.