您的位置:首页 > 理论基础 > 计算机网络

Volley框架支持HTTPS请求。

2015-11-23 16:13 603 查看
android开发中有些时候为了增加安全性会使用HTTPS链接,但是Volley是默认不支持的,也许你会出现一些错误,如下:

requestVolleyError javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

网上有同学是这样处理,http://blog.csdn.net/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());


在测试,成功:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息