【问题标题】:HttpClient strikethrough [duplicate]HttpClient删除线[重复]
【发布时间】:2017-12-30 12:26:49
【问题描述】:

当我尝试创建新的 HttpClient 时,它看起来像这样 ̶H̶t̶t̶p̶C̶l̶i̶e̶n̶t̶

【问题讨论】:

  • 它只是为了承认您已弃用 HttpClient 。如果你愿意,你仍然可以使用它

标签: android httpclient strikethrough


【解决方案1】:

当我尝试创建新的 HttpClient 时,它看起来像这样 H̶t̶t̶p̶C̶l̶i̶e̶n̶t̶

HttpClient 方法已弃用。

你可以使用HttpURLConnection

这里是如何使用HttpURLConnectionAsyncTask 的简单演示代码

        public static class GetAsyncTask extends AsyncTask<Void, Void, String> {


    @Override
    protected String doInBackground(Void... voids) {
        StringBuffer result = new StringBuffer("");


        try {
            URL url = new URL("https://stackoverflow.com/");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// here you can set request method like GET and POST
            httpURLConnection.setConnectTimeout(20000);// here you can set connection time out
            httpURLConnection.setReadTimeout(20000);// here you can set Read Time out
            httpURLConnection.connect();

            InputStream inputStream = httpURLConnection.getInputStream();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                result.append(line);
            }


        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return result.toString();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }



    @Override
    protected void onPostExecute(String result) {

    }
}

比这样使用

GetAsyncTask getAsyncTask = new GetAsyncTask();
    try {
        String str=getAsyncTask.execute().get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

【讨论】:

  • 那我该怎么办,换掉?用什么?
  • 应该在评论中
  • @HossameMakhlof 检查更新的答案
  • @ND1010_ 真是老兄
  • @Perm 谢谢老板! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-14
  • 2022-01-14
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多