org.apache.http.entity.FileEntity 在 Android 6 (Marshmallow) 中被弃用 [英] org.apache.http.entity.FileEntity is deprecated in Android 6 (Marshmallow)

查看:34
本文介绍了org.apache.http.entity.FileEntity 在 Android 6 (Marshmallow) 中被弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将应用升级到 API 23,其中 org.apache.http 已被弃用.

I'm upgrading an app to API 23 where org.apache.http is deprecated.

我当前(已弃用)的代码如下所示:

My current (deprecated) code looks like this:

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 完成此操作的建议,但它们都比当前的解决方案(不能再使用)复杂得多.我说的是用于执行与上述相同功能的多行代码.

I've found some suggestions to how this should be done using HttpURLConnection, but they are all much more complex then the current solution (which cannot be used anymore). I'm talking about many lines of code for executing the same functionality as the above.

示例是:此页面本页

有没有人有一个很好的、更短的解决方案?

Does anyone have a good solid shorter solution for that?

推荐答案

如果您将 compileSdkVersion 更改为 21,您的应用程序将干净利落地编译.话虽如此,Google 退出内置 HttpClient 实现是有原因的,因此您可能应该寻求其他一些库.那其他图书馆"可能是:

If you change your compileSdkVersion to 21, your app will compile cleanly. That being said, there are reasons why Google is backing away from the built-in HttpClient implementation, so you probably should pursue some other library. That "some other library" could be:

  • the built-in classic Java HttpUrlConnection, though as you have found, its API leaves something to be desired
  • Apache's independent packaging of HttpClient for Android
  • OkHttp (my recommendation)
  • AndroidAsync

特别是 OkHttp 似乎有一个非常好的 API 用于发布文件发布多部分表单,这应该类似于您的 HttpClient 代码

In particular, OkHttp seems to have a pretty good API for posting a file and posting a multipart form, which should be similar to what your HttpClient code is doing.

这篇关于org.apache.http.entity.FileEntity 在 Android 6 (Marshmallow) 中被弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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