org.apache.http.entity.FileEntity 在 Android 6(棉花糖)中已弃用
org.apache.http.entity.FileEntity is deprecated in Android 6 (Marshmallow)
我正在将应用升级到 API 23,其中 org.apache.http
已弃用。
我当前(已弃用)的代码如下所示:
HttpClient httpClient = new DefaultHttpClient();
File file = new File(attr.Value);
String url = server_url;
HttpPost request = new HttpPost(url);
FileEntity fileEntity = new FileEntity(file, "image/png");
request.setEntity(fileEntity);
HttpResponse response = httpClient.execute(request);
String output = getContent(response.getEntity().getContent());
我找到了一些关于如何使用 HttpURLConnection
完成此操作的建议,但它们都比当前解决方案(不能再使用)复杂得多。我说的是执行与上述相同功能的许多代码行。
例如:this page and this page
有没有人有一个好的、更短的解决方案?
Apache HttpClient 4.3 port for Android was intended to remedy the
situation by providing official releases compatible with Google
Android.
Given that as of Android API 23 Google's fork of HttpClient has been
removed this project has been discontinued.
Those users who want to continue using Apache HttpClient on Android
are advised to consider
针对 Android 的 Apache HttpClient 4.3 移植 Android API 22 岁及以上
dependencies {
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}
针对 Android 的 Apache HttpClient 软件包由 Marek Sebera 在针对 Android API 23 和更高版本
时维护
dependencies {
compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'
}
取自Apache官方网站:Apache HttpClient for Android
注意: 您不必使用 useLibrary 'org.apache.http.legacy'
语句,该语句是为未从 Android 提供的 HttpClient 类。进一步 explanation.
如果您将 compileSdkVersion
更改为 21,您的应用程序将顺利编译。话虽如此,Google 放弃内置 HttpClient 实现是有原因的,因此您可能应该寻求其他一些库。那
"some other library" 可能是:
- 内置的经典 Java
HttpUrlConnection
,尽管如您所见,它的 API 仍有不足之处
- Apache对HttpClient for Android
的独立封装
- OkHttp(我的推荐)
- AndroidAsync
特别是,OkHttp 似乎对 posting a file and posting a multipart form 有很好的 API,这应该与您的 HttpClient 代码所做的类似。
HTTPClient 的最佳替代品是使用 Volley。它更易于使用,处理请求队列并缓存您的请求。它与几乎所有 API 级别完全兼容,低至 API 4.
查看 Android documentation 如何操作。
我正在将应用升级到 API 23,其中 org.apache.http
已弃用。
我当前(已弃用)的代码如下所示:
HttpClient httpClient = new DefaultHttpClient();
File file = new File(attr.Value);
String url = server_url;
HttpPost request = new HttpPost(url);
FileEntity fileEntity = new FileEntity(file, "image/png");
request.setEntity(fileEntity);
HttpResponse response = httpClient.execute(request);
String output = getContent(response.getEntity().getContent());
我找到了一些关于如何使用 HttpURLConnection
完成此操作的建议,但它们都比当前解决方案(不能再使用)复杂得多。我说的是执行与上述相同功能的许多代码行。
例如:this page and this page
有没有人有一个好的、更短的解决方案?
Apache HttpClient 4.3 port for Android was intended to remedy the situation by providing official releases compatible with Google Android.
Given that as of Android API 23 Google's fork of HttpClient has been removed this project has been discontinued.
Those users who want to continue using Apache HttpClient on Android are advised to consider
针对 Android 的 Apache HttpClient 4.3 移植 Android API 22 岁及以上
dependencies {
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}
针对 Android 的 Apache HttpClient 软件包由 Marek Sebera 在针对 Android API 23 和更高版本
时维护dependencies {
compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'
}
取自Apache官方网站:Apache HttpClient for Android
注意: 您不必使用 useLibrary 'org.apache.http.legacy'
语句,该语句是为未从 Android 提供的 HttpClient 类。进一步 explanation.
如果您将 compileSdkVersion
更改为 21,您的应用程序将顺利编译。话虽如此,Google 放弃内置 HttpClient 实现是有原因的,因此您可能应该寻求其他一些库。那
"some other library" 可能是:
- 内置的经典 Java
HttpUrlConnection
,尽管如您所见,它的 API 仍有不足之处 - Apache对HttpClient for Android 的独立封装
- OkHttp(我的推荐)
- AndroidAsync
特别是,OkHttp 似乎对 posting a file and posting a multipart form 有很好的 API,这应该与您的 HttpClient 代码所做的类似。
HTTPClient 的最佳替代品是使用 Volley。它更易于使用,处理请求队列并缓存您的请求。它与几乎所有 API 级别完全兼容,低至 API 4.
查看 Android documentation 如何操作。