简体   繁体   English

HttpEntity现在在Android上被弃用了,还有什么选择?

[英]HttpEntity is deprecated on Android now, what's the alternative?

With the release of Android 5.1, it looks like all the Apache http stuff has been deprecated. 随着Android 5.1的发布,看起来所有Apache http的东西都被弃用了。 Looking at the documentation is useless; 看文档是没用的; they all say 他们都说

This class was deprecated in API level 22. Please use openConnection() instead. Please visit this webpage for further details.

Which is fine the first time you read it, but when EVERY class that was deprecated says that, it's not that helpful. 这是第一次阅读它时没关系,但是当每个被弃用的类都说明了这一点时,它没那么有用。

Anyway, what are the alternatives for classes like HttpEntity , specifically StringEntity , and MultipartEntity ? 无论如何,像HttpEntity ,特别是StringEntityMultipartEntity这样的类有哪些替代方案? 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 . 我取代BasicNameValuePair为我自己的实现Android的的Pair<T, S>类,它看起来像URLEncoder.encode是一个很好的替代品URLEncodedUtils ,但我不知道该怎么办HttpEntity

EDIT 编辑

I've decided just to re-write the networking stuff. 我决定重新编写网络内容。 Gonna try to use Retrofit and OkHttp 要尝试使用Retrofit和OkHttp

EDIT 编辑

Seriously take a look at switching your calls and stuff to Retrofit. 认真看看将你的通话和东西转换为Retrofit。 Pretty nifty. 很漂亮。 I'm glad I did. 我很高兴我做到了。 There were a few hurdles, but it's cool. 有一些障碍,但它很酷。

You can always import the last Apache Http client and use that. 您始终可以导入最后一个Apache Http客户端并使用它。 Also, you might want to take a look at a networking library like Volley or Retrofit , just in case you can use that instead. 此外,您可能需要查看一个像VolleyRetrofit这样的网络库,以防您可以使用它。 If starting a new project, using a networking library is recommended because there is no need to reinvent the wheel. 如果启动新项目,建议使用网络库,因为不需要重新发明轮子。 But if you are stuck with using HttpClient , then read on. 但是如果你坚持使用HttpClient ,那么请继续阅读。

EDIT: Latest news on Apache HttpClient (as of 11/07/2015) 编辑:关于Apache HttpClient的最新消息(截至2015年7月11日)

Google Android 1.0 was released with a pre-BETA snapshot of Apache HttpClient. 谷歌Android 1.0发布了Apache HttpClient的BETA前快照。 To coincide with the first Android release Apache HttpClient 4.0 APIs had to be frozen prematurely, while many of interfaces and internal structures were still not fully worked out. 为了配合第一个Android版本,Apache HttpClient 4.0 API必须过早冻结,而许多接口和内部结构仍未完全解决。 As Apache HttpClient 4.0 was maturing the project was expecting Google to incorporate the latest code improvements into their code tree. 随着Apache HttpClient 4.0的日趋成熟,该项目期待Google将最新的代码改进整合到他们的代码树中。 Unfortunately it did not happen. 不幸的是没有发生。 Version of Apache HttpClient shipped with Android has effectively become a fork. Android附带的Apache HttpClient版本实际上已成为一个分支。 Eventually Google decided to discontinue further development of their fork while refusing to upgrade to the stock version of Apache HttpClient citing compatibility concerns as a reason for such decision. 最终谷歌决定停止进一步开发他们的分支,同时拒绝升级到Apache HttpClient的股票版本,理由是兼容性问题是这样决定的原因。 As a result those Android developers who would like to continue using Apache HttpClient APIs on Android cannot take advantage of newer features, performance improvements and bug fixes. 因此,那些希望在Android上继续使用Apache HttpClient API的Android开发人员无法利用更新的功能,性能改进和错误修复。 Apache HttpClient 4.3 port for Android was intended to remedy the situation by providing official releases compatible with Google Android. 用于Android的Apache HttpClient 4.3端口旨在通过提供与Google Android兼容的官方版本来解决这种情况。 Given that as of Android API 23 Google's fork of HttpClient has been removed this project has been discontinued. 鉴于从Android API 23开始,谷歌的HttpClient分支已被删除,该项目已经停止。

However, there is an official Android port of the Apache HttpClient v4.3 但是,有一个Apache HttpClient v4.3的官方Android端口

Android API 22 and older should use the Apache HttpClient v4.3 Android API 22及更早版本应使用Apache HttpClient v4.3

dependencies {
         compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' 
}

Android API 23 and newer should use Apache HttpClient packages for Android maintained by Marek Sebera Android API 23及更新版本应使用由Marek Sebera维护的适用于Android的Apache HttpClient软件包

dependencies {
     compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1' 
}

Info taken from Apache.org 来自Apache.org的信息

The HttpClient documentation points you in the right direction: HttpClient文档指出了正确的方向:

org.apache.http.client.HttpClient : org.apache.http.client.HttpClient

This interface was deprecated in API level 22. Please use openConnection() instead. 此接口在API级别22中已弃用。请改用openConnection()。 Please visit this webpage for further details. 请访问此网页了解更多详情。

means that you should switch to java.net.URL.openConnection() . 意味着你应该切换到java.net.URL.openConnection()

Here's how you could do it: 这是你如何做到的:

java.net.URL url = new java.net.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 = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
System.out.println(response);

Call webservice httpclient replace by httpurlconnection 通过httpurlconnection调用webservice httpclient替换

my code here 我的代码在这里

public static String getDataFromUrl(String url) {
        String result = null;
//        System.out.println("URL comes in jsonparser class is:  " + url);
        try {
            URL myurl=new URL(url);
            HttpURLConnection urlConnection = (HttpURLConnection) myurl
                    .openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoInput(true);
            urlConnection.connect();
            InputStream is=urlConnection.getInputStream();
            if (is != null) {
                StringBuilder sb = new StringBuilder();
                String line;
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is));
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }
                    reader.close();
                } finally {
                  is.close();
                }
                result = sb.toString();
            }
        }catch (Exception e){
            result=null;
        }
        return result;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:StatusLine 现在已弃用,有什么替代方案? - Android: StatusLine now deprecated, what is the alternative? Android中不推荐使用的AbsoluteLayout替代品是什么? - What's alternative to deprecated AbsoluteLayout in Android? showDialog已弃用。有什么选择? - showDialog deprecated. What's the alternative? Koin 的 getStateViewModel 已被弃用 - 有什么替代方案? - Koin's getStateViewModel is deprecated - what is the alternative? android中已弃用的cachemanager类的替代方法是什么? - what is the alternative for the deprecated cachemanager class in android Android getAllNetworkInfo() 已弃用。 有什么选择? - Android getAllNetworkInfo() is Deprecated. What is the alternative? Android windowInsets.getStableInsetBottom 在 Android 中被弃用 11. 它的替代品是什么? - Android windowInsets.getStableInsetBottom got deprecated in Android 11. What's the alternative for it? Android Deprecated 注解已弃用,用什么替代? - Android Deprecated Annotation is deprecated, what's the replacement? getSerializableExtra 已弃用,有什么替代方案? - getSerializableExtra deprecated, What is the alternative? onBackPressed() 已弃用,有什么替代方案? - onBackPressed() deprecated, What is the alternative?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM