安卓M:org.apache.http.entity.FileEntity德precated [英] Android M: org.apache.http.entity.FileEntity deprecated

查看:251
本文介绍了安卓M:org.apache.http.entity.FileEntity德precated的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序升级到23 API,其中 org.apache.http 是德precated。

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

我现在的(de precated)code样子的:

My current (deprecated) code looks like that:

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 完成,但他们都复杂得多,那么当前的解决方案(不能再使用)。我说的是code多行执行相同的功能上面。

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.

的例子是:此页面和<一href="http://stackoverflow.com/questions/11766878/sending-files-using-post-with-httpurlconnection">this页面

有没有人有一个良好的坚实较短的解决方案是什么?

Does anyone have a good solid shorter solution for that?

推荐答案

如果您在 compileSdkVersion 更改为21,你的应用程序将汇编干净。话虽这么说,是有原因的谷歌正在背弃内置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似乎有对的发布的文件发布一个多形式,这应该是类似于你的HttpClient code在做什么。

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.

这篇关于安卓M:org.apache.http.entity.FileEntity德precated的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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