简体   繁体   English

java.lang.ClassCastException:libcore.net.http.HttpURLConnectionImpl无法强制转换为javax.net.ssl.HttpsURLConnection

[英]java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection

I'm writing a bit of code to upload a file from the device to the cloud over HTTPS. 我正在编写一些代码,通过HTTPS将文件从设备上传到云端。

Relevant snippet: 相关片段:

HttpsURLConnection conn = null; 
URL url = new URL(urlstring);
conn = (HttpsURLConnection) url.openConnection(); // exception here.

But the cast won't compile: 但演员阵容不会编译:

06-20 15:58:05.311: E/FNF(30286): java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection

I found this similar question: Using java class HttpsURLConnection , but I am not importing anything from the sun package. 我发现了类似的问题: 使用java类HttpsURLConnection ,但我没有从sun包导入任何东西。

My imports: 我的进口:

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import android.net.Uri;
import javax.net.ssl.HttpsURLConnection;
import android.util.Log;
import edu.mit.media.funf.storage.RemoteFileArchive;
import edu.mit.media.funf.util.LogUtil;

I've been scratching my head about this one for a while now, any suggestions? 我一直在讨论这个问题一段时间了,有什么建议吗?

Method 1: Your urlString must begin with https:// and not http:// for you to be able to cast it to a HttpsURLConnection . 方法1:您的urlString必须以https://开头而不是http://以便您能够将其HttpsURLConnection转换为HttpsURLConnection

Method 2: if your urlString starts with http:// , changing HttpsURLConnection to HttpURLConnection should work 方法2:如果您的urlString以http://开头,则将HttpsURLConnection更改为HttpURLConnection应该可以正常工作

I had same Exception java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection 我有同样的异常java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection

uri = new URL("http://www.google.com");
HttpsURLConnection connection = (HttpsURLConnection) uri.openConnection(); // Exception

I changed 我变了

uri = new URL("http://www.google.com");

to

uri = new URL("https://www.google.com");

Now it is working perfectly. 现在它工作得很好。

url.openConnection(); seems to be returning an object of type libcore.net.http.HttpURLConnectionImpl while you have declared your "conn" object as being of type import javax.net.ssl.HttpsURLConnection; 似乎是在返回一个libcore.net.http.HttpURLConnectionImpl类型的对象,而你已经将你的“conn”对象声明为import javax.net.ssl.HttpsURLConnection; . You need to sort up your imports and used objects. 您需要对导入和使用的对象进行排序。 Maybe you missed something in the tutorial you were following. 也许你错过了你所关注的教程中的内容。

Simple remove urlConnection.setDoOutput(true); 简单删除urlConnection.setDoOutput(true);

it will work fine. 它会工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerMock java.lang.ClassCastException:sun.net.www.protocol.https.HttpsURLConnectionImpl无法强制转换为javax.net.ssl.HttpsURLConnection - PowerMock java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl 无法转换为 javax.net.ssl.HttpsURLConnection - java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl cannot be cast to javax.net.ssl.HttpsURLConnection weblogic.net.http.SOAPHttpsURLConnection 无法转换为 javax.net.ssl.HttpsURLConnection - weblogic.net.http.SOAPHttpsURLConnection cannot be cast to javax.net.ssl.HttpsURLConnection 转换异常:HttpsURLConnectionOldImpl v javax.net.ssl.HttpsURLConnection - Cast exception: HttpsURLConnectionOldImpl v javax.net.ssl.HttpsURLConnection javax.net.ssl.HttpsURLConnection何时触发请求 - javax.net.ssl.HttpsURLConnection when are the request fired 返回火星诗歌的javax.net.ssl.HttpsURLConnection - javax.net.ssl.HttpsURLConnection returning martian poetry javax.net.ssl.HttpsURLConnection vs org.apache.http.client.HttpClient - javax.net.ssl.HttpsURLConnection vs org.apache.http.client.HttpClient Google App Engine和openid4java-javax.net.ssl.HttpsURLConnection是受限制的类 - Google App Engine and openid4java - javax.net.ssl.HttpsURLConnection is a restricted class java.lang.ClassCastException:无法转换为javax.servlet.Servlet - java.lang.ClassCastException: cannot be cast to javax.servlet.Servlet 引起:java.lang.ClassCastException:使用Generic Type时,libcore.reflect.ParameterizedTypeImpl无法强制转换为java.lang.Class - Caused by: java.lang.ClassCastException: libcore.reflect.ParameterizedTypeImpl cannot be cast to java.lang.Class when use Generic Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM