在 API 级别 23 中删除了 org.apache.http 包.有什么替代方案? [英] org.apache.http packages removed in API level 23. What is the alternative?

查看:54
本文介绍了在 API 级别 23 中删除了 org.apache.http 包.有什么替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更新到最新的 android API 级别 23 (Marshmallow) 后立即通过 build.gradle 添加以下更改后,所有 org.apache.http 类都不起作用.

After adding following changes in by build.gradle right after updating to latest android API level 23 (Marshmallow) all org.apache.http classes is not working.

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.myapp.package"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 2
        versionName "1.1"
    }
}

我查看了Android API 差异报告"这里.它说 org.apache.http 的所有类已被删除.有人可以建议什么是替代方案吗?

I checked "Android API Differences Report" here. Its says all Classes of org.apache.http has been removed. Can someone suggest what is the alternative ?

这是我的代码:

try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(address);

            httpPost.setEntity(new StringEntity("{\"longUrl\":\""+longUrl+"\"}"));
            httpPost.setHeader("Content-Type", "application/json");
            HttpResponse httpResponse = httpClient.execute(httpPost);

            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

推荐答案

参见 Android Developers 上的 >Behavior Changes 说明:

Android 6.0 版本取消了对 Apache HTTP 客户端的支持.如果你的应用正在使用此客户端并针对 Android 2.3(API 级别 9)或更高版本,改用 HttpURLConnection 类.这个API效率更高因为它通过透明压缩减少了网络使用,并且响应缓存,并最大限度地降低功耗.继续使用Apache HTTP API,您必须首先声明以下内容build.gradle 文件中的编译时依赖项:

Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption. To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:

android {
    useLibrary 'org.apache.http.legacy' 
} 

这篇关于在 API 级别 23 中删除了 org.apache.http 包.有什么替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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