2013-07-03 27 views
0

我收到以下错误试图跟随How to add parameters to HttpURLConnection using POST人有任何想法,为什么这是轰炸了参数添加到HttpURLConnection的

的logcat:

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) 

的Java:

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(); 
    } 

回答

1

看起来你的网址不是以https开头的。更改网址以https开头,它应该工作。

+0

你绝对正确 – jcaruso