HttpEntity是pcated在Android现在去$ P $,有什么替代方案? [英] HttpEntity is deprecated on Android now, what's the alternative?
问题描述
使用的Android 5.1的版本,它看起来像所有的Apache HTTP东西已经pcated德$ P $。纵观文件是没有用的;他们都说
With the release of Android 5.1, it looks like all the Apache http stuff has been deprecated. Looking at the documentation is useless; they all say
这个类是pcated在API层面22日$ P $。
请使用的openConnection()来代替。请访问此网页了解更多详情。
This class was deprecated in API level 22.
Please use openConnection() instead. Please visit this webpage for further details.
这很好你第一次读它,但是当每一个类,它是pcated德$ P $说,它不是有益的。
Which is fine the first time you read it, but when EVERY class that was deprecated says that, it's not that helpful.
总之,什么是类的替代品 HttpEntity
像,特别是 StringEntity
和 MultipartEntity
我取代 BasicNameValuePair </ code>为我自己的实现Android的
对&LT的; T,S&GT;
类,它看起来像 URLEn coder.en code
是一个很好的替代品 URLEn codedUtils
,但我不知道该怎么办 HttpEntity
。
Anyway, what are the alternatives for classes like HttpEntity
, specifically StringEntity
, and MultipartEntity
? I substituted BasicNameValuePair
for my own implementation of Android's Pair<T, S>
class, and it looks like URLEncoder.encode
is a good substitute for URLEncodedUtils
, but I'm not sure what to do about HttpEntity
.
修改
我已经决定只是重新写网络的东西。要去尝试使用改造和OkHttp
I've decided just to re-write the networking stuff. Gonna try to use Retrofit and OkHttp
修改
认真看看切换你的电话和东西改造。 pretty的漂亮。我很高兴我做到了。当时有一些障碍,但它的凉爽。
Seriously take a look at switching your calls and stuff to Retrofit. Pretty nifty. I'm glad I did. There were a few hurdles, but it's cool.
推荐答案
在 HttpClient的文件指向你在正确的方向:
The HttpClient documentation points you in the right direction:
org.apache.http.client.HttpClient
:
该接口是pcated在API层面22日$ P $。 请使用的openConnection()来代替。请访问此网页了解更多详情。
This interface was deprecated in API level 22. Please use openConnection() instead. Please visit this webpage for further details.
意味着你应该切换到 java.net.URL.openConnection()
。
下面是你如何能做到这一点:
Here's how you could do it:
URL url = new URL("http://some-server");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// read the response
System.out.println("Response Code: " + conn.getResponseCode());
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = IOUtils.toString(in, "UTF-8");
System.out.println(response);
URL= java.net.URL
IOUtils= org.apache.commons.io.IOUtils
这篇关于HttpEntity是pcated在Android现在去$ P $,有什么替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!