【问题标题】:How to get data from JSON - HttpClient deprecated [duplicate]如何从 JSON 获取数据 - 不推荐使用 HttpClient [重复]
【发布时间】:2015-09-07 22:57:57
【问题描述】:

我曾经从下面的代码中获取 JSON 数据,但现在 HttpClient 已弃用,我不能再这样做了。你能帮帮我吗?

String responseString = null;
    try {
        HttpClient client = new DefaultHttpClient();
        URI apiCall = new URI("...");
        HttpGet request = new HttpGet();
        request.setURI(apiCall);
        HttpResponse response = client.execute(request);
        responseString = EntityUtils.toString(response.getEntity());
    } catch (Exception e) {
        Log.e("TAG", "some sort of problem encountered", e);
    }

【问题讨论】:

标签: android json httpclient


【解决方案1】:

使用另一个类,如

   URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);
    finally {
     urlConnection.disconnect();
   }
 }

【讨论】:

  • 我怎样才能让它适应 AsyncTask?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 2015-09-16
  • 2019-04-25
  • 1970-01-01
  • 2020-09-13
  • 2015-07-05
  • 1970-01-01
相关资源
最近更新 更多