Post

Volley自定义中的几个问题的原因

打算应用Volley一下,优势我也没有感觉出来太多,在封装过程中遇到几个问题记录一下,以后更新使用感想吧,注意我这里有些问题是不考虑兼容2.3及以前的版本的

如何添加参数

很简单重写getParams方法

1
2
3
4
@Override
public Map<String, String> getParams() {
   return mParams;
}

如何使用Cookie

HttpURLConnection已经直接支持了,HttpURLConnection

1
2
CookieHandler.setDefault(new CookieManager()); 
CookieHandler.setDefault( new CookieManager( null, CookiePolicy.ACCEPT_ALL ) );
  • Prepare the request. The primary property of a request is its URI. Request headers may also include metadata such as credentials, preferred content types, and session cookies.
  • Read the response. Response headers typically include metadata such as the response body’s content type and length, modified dates and session cookies. The response body may be read from the stream returned by getInputStream(). If the response has no body, that method returns an empty stream.

com.android.volley.Cache$Entry 抛出来的空指针

1
2
3
Attempt to read from field com.android.volley.Cache$Entry NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException
Attempt to read from field com.android.volley.Cache$Entry com.android.volley.Response.cacheEntryon a null object reference java.lang.NullPointerException: 
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)
1
2
3
4
5
6
//must return cache
 @Override
 protected Response<String> parseNetworkResponse(NetworkResponse response) {
	...
	return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}

int statusCode = statusLine.getStatusCode();抛NullPointerException

如果Request中的参数有为null的就可能报这个错误,比如listener为null

Set Timeout Time

1
2
3
4
myRequest.setRetryPolicy(new DefaultRetryPolicy(
                MY_SOCKET_TIMEOUT_MS, 
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
This post is licensed under CC BY 4.0 by the author.