[英]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.