org.apache.http包在API级别23.什么是另类删除? [英] org.apache.http packages removed in API level 23. What is the alternative?
问题描述
更新到最新的Android API级别23(棉花糖)所有org.apache.http类是不工作之后将通过下面的build.gradle更改后。
安卓{
compileSdkVersion 23
buildToolsVersion23.0.0 defaultConfig {
的applicationIDcom.myapp.package
15的minSdkVersion
targetSdkVersion 23
版本code 2
的versionName1.1
}
}
我查了Android的API差异报告<一个href=\"http://developer.android.com/sdk/api_diff/23/changes.html?utm_campaign=m-developer-launch&utm_source=dac&utm_medium=blog\">here.它说,org.apache.http各阶层已被删除。有人建议可以有什么选择?
下面是我的code:
{尝试 DefaultHttpClient的HttpClient =新DefaultHttpClient();
HttpPost httpPost =新HttpPost(地址); httpPost.setEntity(新StringEntity({\\longUrl \\:\\+ longUrl +\\}));
httpPost.setHeader(内容类型,应用/ JSON);
HTT presponse HTT presponse = httpClient.execute(httpPost); HttpEntity httpEntity = HTT presponse.getEntity();
是= httpEntity.getContent(); }赶上(UnsupportedEncodingException五){
e.printStackTrace();
}赶上(ClientProtocolException E){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
查看的行为在Android开发者更改它说:
的Android 6.0版本删除了的Apache HTTP客户端支持。如果您的应用程序
使用此客户端和目标的Android 2.3(API等级9)或更高,
使用HttpURLConnection的类代替。此API是更有效的
因为它减少网络使用通过透明的COM pression和
响应缓存,并最大限度地减少功耗。要继续使用
Apache的HTTP API,您必须首先声明以下
编译时依赖在你的build.gradle文件:安卓{
useLibraryorg.apache.http.legacy
}
块引用>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" } }
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 ?
Here is my code :
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(); }
解决方案See the Behavior Changes at Android Developers where it says that:
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' }
这篇关于org.apache.http包在API级别23.什么是另类删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!