0

我一直在寻找很多,但没有找到任何解决方案。我想使用URLHttpsUrlConnection而不是已弃用的 ( HttpClient, HttpPost, DefaultHttpClient)。到目前为止,我有以下代码:

注意“MyUrl”需要一些参数。见问题 2。

 URL url = new URL("MyUrl");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");

    /*Here exception occurs!*/
    connection.connect();

所以,我有两个问题要解决:(也许第二个应该首先解决。我不知道......)

  1. 当我使用connection.someMethod();SSLException 时发生。(即connection.getResponseCode();

错误是:

javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x635cb550: Failure in SSL library, usually a protocol error

  1. 什么是替代方式List<NameValuePair> & BasicNameValuePair?这些也已被弃用。
4

2 回答 2

1

您可以将以下库用于HTTP 请求响应

您可以使用Google-Gson来解析 JSON 数据

于 2016-01-07T07:53:24.323 回答
0

您不需要使用连接。只需这样做:

 conn.setRequestMethod("POST");
 InputStream in = new BufferedInputStream(conn.getInputStream());
 response = org.apache.commons.io.IOUtils.toString(in, "UTF-8"); 

你会得到你的回应。

此外,您可以使用 ContentValues 作为 NameValuePair 的替代品:

ContentValues initialValues = new ContentValues();
initialValues.put("parameter", value);
于 2016-01-07T08:24:56.570 回答