本文介绍了java.lang.ClassCastException:libcore.net.http.HttpURLConnectionImpl无法转换为javax.net.ssl.HttpsURLConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码,以通过HTTPS将文件从设备上传到云.

I'm writing a bit of code to upload a file from the device to the cloud over HTTPS.

相关代码段:

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

我发现了类似的问题:使用Java类HttpsURLConnection ,但我没有导入任何内容从太阳包.

I found this similar question: Using java class HttpsURLConnection, but I am not importing anything from the sun package.

我的进口商品:

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?

推荐答案

方法1:您的urlString必须以https://而不是http://开头,才能将其转换为HttpsURLConnection.

Method 1:Your urlString must begin with https:// and not http:// for you to be able to cast it to a HttpsURLConnection.

方法2:如果您的urlString以http://开头,则将HttpsURLConnection更改为HttpURLConnection应该可以

Method 2:if your urlString starts with http://, changing HttpsURLConnection to HttpURLConnection should work

这篇关于java.lang.ClassCastException:libcore.net.http.HttpURLConnectionImpl无法转换为javax.net.ssl.HttpsURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:36
查看更多