While integrating Volley into a project, I encountered several common issues worth documenting. The following does not apply to Android 2.3 and below.
Adding Request Parameters#
Override getParams:
1
2
3
4
| @Override
protected Map<String, String> getParams() {
return mParams;
}
|
Using Cookies#
HttpURLConnection natively supports cookie management via CookieHandler:
1
2
| CookieHandler.setDefault(new CookieManager());
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
|
Cookies are carried in request headers and parsed from set-cookie in responses. See the HttpURLConnection documentation.
Cache.Entry NullPointerException#
1
2
3
| Attempt to read from field com.android.volley.Cache$Entry
com.android.volley.Response.cacheEntry on a null object reference
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:126)
|
Cause: Passing null as the second parameter in parseNetworkResponse causes a NPE when NetworkDispatcher reads the cache entry.
Fix: Always return valid cache headers.
1
2
3
4
5
| @Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
// ...
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
|
statusCode NullPointerException#
1
| int statusCode = statusLine.getStatusCode(); // NullPointerException
|
This can occur when constructor parameters (e.g., listener) are null. Ensure all required parameters are non-null.
Setting Timeout#
1
2
3
4
| myRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
|
References#