2013-07-03 49 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