Android 已弃用 apache 模块(HttpClient、HttpResponse 等)

标签 android apache apache-commons-httpclient

自 API 级别 22 起,Android 已弃用 Apache 模块,所以我的问题是,我该如何使用,例如来自 Apache 库而不是 Android SDK 的 HttpResponse?问题是这两个包都是一样的。

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

最佳答案

方法 HttpClient 已被弃用。您现在可以使用 URLConnection,如本例所示:

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

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

关于Android 已弃用 apache 模块(HttpClient、HttpResponse 等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29294479/

相关文章:

android - AsyncTask 运行时的 Espresso 测试后退按钮

android - 更新数据库安卓

PHP 脚本运行两次

amazon-web-services - Certbot 无法找到环境变量凭据

java - 哪个 API 将 Java List<Object> 转换为 HTTP 表单参数?

java - 将 HttpClient 与 SSL 和证书结合使用

android - 如何磨练我的安卓开发技能?

android - Asynctask 不会更新数组

PHP - LDAP 搜索偶尔通过 TLS/SSL 工作

java - 如何使用 Apache HttpClient 发布 JSON 请求?