android开发中有些时候为了增加安全性会使用HTTPS链接,但是Volley是默认不支持的,也许你会出现一些错误,如下:
requestVolleyError javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
网上有同学是这样处理,http://blog.youkuaiyun.com/llwdslal/article/details/18052723
这个方式是可以实现的,如果你使用的JAR包的话,稍微有点麻烦。查
VolleyAPI发现newRequestQueue的构造函数支持自定义的HurlStack。
既然这个,我就自定义了一个HurlStack
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import com.android.volley.toolbox.HurlStack;
public class TTHurlStack extends HurlStack {
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
if (url.toString().contains("https")) {
HTTPSTrustManager.allowAllSSL();
}
return super.createConnection(url);
}
}
并且在Volley的修改创建方法
mRequestQueue = Volley.newRequestQueue(context,new TTHurlStack());
在测试,成功:)