gpt4 book ai didi

java - API 级别 23 中删除了 org.apache.http 包。有什么替代方案?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:57:22 26 4
gpt4 key购买 nike

在更新到最新的 android API 级别 23 (Marshmallow) 之后,通过 build.gradle 添加以下更改后,所有 org.apache.http 类都无法正常工作。

android {
compileSdkVersion 23
buildToolsVersion "23.0.0"

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

我检查了“Android API 差异报告”here .它说 org.apache.http 的所有类都已被删除。有人可以建议替代方案是什么吗?

这是我的代码:

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

最佳答案

参见 Behavior Changes在 Android Developers 那里说:

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

关于java - API 级别 23 中删除了 org.apache.http 包。有什么替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32202489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com