I was comparing the Volley Libs (the old one I had and the latest one here)
Below is whats used for instantiating the Request (Request.java class)
//Old code
public Request(int method, String url, Response.ErrorListener listener,int requestCode) {
mMethod = method;
mUrl = url;
mErrorListener = listener;
mRequestCode = requestCode;
setRetryPolicy(new DefaultRetryPolicy());
mDefaultTrafficStatsTag = TextUtils.isEmpty(url) ? 0: Uri.parse(url).getHost().hashCode();
}
//Current code
public Request(int method, String url, Response.ErrorListener listener) {
mMethod = method;
mUrl = url;
mIdentifier = createIdentifier(method, url);
mErrorListener = listener;
setRetryPolicy(new DefaultRetryPolicy());
mDefaultTrafficStatsTag = findDefaultTrafficStatsTag(url);
}
What came to my surprise is something I have been using very prominently. the requestCode variable missing in the new one using which I used to identify my response or error belongs to which Request.
A new variable mIdentifier has been introduced which I thought might be the replacement, but couldn't find anything about it.
Also, I found the setTag()
method being used on the Request object but that's only for cancelling the request, here.
Any suggestion or help on how I can implement the same requestCode thing or a workaround to get the same functionality in the new code will be great.
Thanks. :)
Sample: Below is the way response and error callbacks were handled. Missing the type params in the old code.
//Old code
/** Callback interface for delivering parsed responses. */
public interface Listener<T> {
/** Called when a response is received. */
public void onResponse(T response,int type);
/**
* On response headers.
*
* @param headers the headers
*/
public void onResponseHeaders(Map<String, String> headers,int type);
}
/** Callback interface for delivering error responses. */
public interface ErrorListener {
/**
* Callback method that an error has been occurred with the
* provided error code and optional user-readable message.
*/
public void onErrorResponse(VolleyError error,int type);
}
//Current code
/** Callback interface for delivering parsed responses. */
public interface Listener<T> {
/** Called when a response is received. */
public void onResponse(T response);
}
/** Callback interface for delivering error responses. */
public interface ErrorListener {
/**
* Callback method that an error has been occurred with the
* provided error code and optional user-readable message.
*/
public void onErrorResponse(VolleyError error);
}
Edit (I will like to achieve the below in the new code rather than writing the response every single time I create a request):
@Override
public void onResponse(Object response, int type) {
if (type == NetworkController.REQ_CODE_UPDATE_USER) {
//Update user
}else if(type == NetworkController.REQ_CODE_DELETE_USER){
//Delete User
}
}
If i understood correctly, you wanted to maintain a single response listener for all the requests you create. Then you can implement a custom listener like below and store the reqcode for each request and handle the response based on reqcode.
Create a Request as below.
I Hope this is what you needed.