Android 弃用 apache 模块(HttpClient、HttpResponse 等) [英] Android deprecated apache module (HttpClient, HttpResponse, etc.)

查看:40
本文介绍了Android 弃用 apache 模块(HttpClient、HttpResponse 等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 自 API 级别 22 起已弃用 Apache 模块,所以我的问题是,我该如何使用,例如 HttpResponse 来自 Apache 库,而不是来自 Android SDK?问题是这两个包中的内容相同.

Android has deprecated the Apache module since API level 22, so my question is, how do I use, for example HttpResponse from the Apache library, not from Android SDK? The problem is that the're the same in both packages.

但是,例如,HttpGet 是可以的,因为它在 Apache 中被称为 HttpGetHC4.

But, for example, HttpGet is OK, because it's called HttpGetHC4 in Apache.

推荐答案

不推荐使用 HttpClient 方法.您现在可以使用 URLConnection,如本示例中所示:

The method HttpClient was deprecated. You can now use the URLConnection as you can see in this example:

private StringBuffer request(String urlString) {
    // TODO Auto-generated method stub

    StringBuffer chaine = new StringBuffer("");
    try{
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestProperty("User-Agent", "");
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.connect();

        InputStream inputStream = connection.getInputStream();

        BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        while ((line = rd.readLine()) != null) {
            chaine.append(line);
        }
    }
    catch (IOException e) {
        // Writing exception to log
        e.printStackTrace();
    }
    return chaine;
}

我希望这对某人有所帮助.

I hope this is helping someone.

这篇关于Android 弃用 apache 模块(HttpClient、HttpResponse 等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆