【问题标题】:Alternative for deprecated HttpClient, HttpPost, NameValuePair [duplicate]替代已弃用的 HttpClient、HttpPost、NameValuePair [重复]
【发布时间】:2016-01-07 07:32:19
【问题描述】:

我已经搜索了很多,但没有找到任何解决方案。我想使用 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 的替代方法是什么?这些也已被弃用。

【问题讨论】:

标签: android httpsurlconnection basicnamevaluepair


【解决方案1】:

您可以将以下库用于HTTP requests-response

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      相关资源
      最近更新 更多