<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">今天在写一个app时,当实现从网络上获取图片资源,发送HTTPURLConnection的时候抛出这样的异常:</span></span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;"> java.lang.ClassCastException: com.android.okhttp.internal.http.HttpURLConnectionImpl cannot be cast to </span></span><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">javax.net.ssl.HttpsURLConnection;</span>经过一番上网搜索后总算发现了问题在哪(PS:看过之后估计你想撞墙的心都有了。。。):
先来说下我的问题吧。
问题描述如下:
之所以会出现那样的异常就在于下面这段代码的错误
 
URL url = new URL(urlString);
HttpsURLConnection connection= (HttpsURLConnection) url.openConnection();
问题原因:你的urlString必须以这种形式开头:“http://” 而不是“https://” 这样你才能匹配HttpURLConnection.
解决办法:将HttpsURLConnection改为HttpURLConnection即可;
解释:
SSL(Secure Sockets Layer 安全套接层)
HTTPS(Secure Hypertext Transfer Protocol)安全超文本传输协议
这两者是相互对应的。