安卓M:org.apache.http.entity.FileEntity德precatedhttp、apache、org、precated

2023-09-13 23:59:01 作者:为将来而努力

我的应用程序升级到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.

的例子是:此页面和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:

内置经典的Java 的HttpURLConnection ,但正如你已经发现,它的API叶不满意 为Android Apache的独立包装的HttpClient OkHttp (我的建议) AndroidAsync 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.