0

How to add parameters to HttpURLConnection using POST をフォローしようとすると、次のエラーが発生します

ログキャット:

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

1 に答える 1

1

URL が https で始まっていないようです。URL を https で始まるように変更すると、機能するはずです。

于 2013-07-03T14:06:55.703 に答える