我在尝试遵循如何使用 POST 向 HttpURLConnection 添加参数时遇到以下错误,任何人都知道为什么会这样
日志猫:
07-03 08:58:54.777: E/AndroidRuntime(26460): java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection
07-03 08:58:54.777: E/AndroidRuntime(26460): at com.projectcaruso.naturalfamilyplaning.HttpPostThread.run(HttpPostThread.java:48)
爪哇:
private URL url;
public HttpPostThread(URL sERVICE_URL, ArrayList<NameValuePair> pairs, final Handler handler)
{
Log.i("PROJECTCARUSO", "Posting to URL: " + sERVICE_URL);
this.url =sERVICE_URL;
this.pairs = pairs;
if(pairs==null){
Log.i("PROJECTCARUSO", "URL parms were null");
this.pairs = new ArrayList<NameValuePair>();
}
}
@Override
public void run() {
try {
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (NameValuePair nvp : pairs) {
//you need to encode ONLY the values of the parameters
params.add(new BasicNameValuePair(nvp.getName(), nvp.getValue()));
Log.i("PROJECTCARUSO", "NVP: " + nvp.getName() + " - " + nvp.getValue());
}
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.close();
os.close();
conn.connect();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}