简体   繁体   English

org.apache.http 已弃用,该使用什么?

[英]org.apache.http is deprecated, what to use?

I have an app that in order to download Json data uses org.apache.http this is the class I use in order to make the request:我有一个应用程序,为了下载Json数据使用org.apache.http这是我用来发出请求的类:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

@SuppressWarnings("deprecation")
public class ServiceHandler {

    static String response = null;
    public final static int GET = 1;
    public final static int POST = 2;

    public ServiceHandler() {

    }

    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public String makeServiceCall(String url, int method) {
        return this.makeServiceCall(url, method, null);
    }

    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * */
    public String makeServiceCall(String url, int method,
            List<NameValuePair> params) {
        try {
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }

                httpResponse = httpClient.execute(httpPost);

            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);

                httpResponse = httpClient.execute(httpGet);

            }
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity,HTTP.UTF_8);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;

    }
}

The problem is that every import from org.apache.http is deprecated and I don't know if i'll have problem using this class.问题是从org.apache.http每个导入都被弃用了,我不知道我是否会在使用这个类时遇到问题。 Can someone point me to the right direction in order to "update" my class using non-deprecated methods?有人可以指出我正确的方向,以便使用未弃用的方法“更新”我的课程吗?

EDIT: From the Android M documentation:编辑:从 Android M 文档:

This preview removes support for the Apache HTTP client.此预览版取消了对 Apache HTTP 客户端的支持。 If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead.如果您的应用使用此客户端并面向 Android 2.3(API 级别 9)或更高版本,请改用 HttpURLConnection 类。 This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption.此 API 更高效,因为它通过透明压缩和响应缓存减少了网络使用,并最大限度地降低了功耗。 To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:要继续使用 Apache HTTP API,您必须首先在 build.gradle 文件中声明以下编译时依赖项:

android {
    useLibrary 'org.apache.http.legacy'
}

So my app will crash using that class right?所以我的应用程序会在使用该类时崩溃,对吗?

Instead of DefaultHttpClient use 代替DefaultHttpClient使用

HttpClient httpClient = HttpClientBuilder.create().build();

And Instead of HTTP.UTF_8 use 并且代替HTTP.UTF_8使用

StandardCharsets.UTF_8

您可以去那里查找不赞成使用的注释,以便您使用

For HTTP.UTF_8 the alternative is Consts.UTF_8. 对于HTTP.UTF_8,替代方法是Consts.UTF_8。 It's weird they don't mention that in the docs. 他们在文档中没有提到这很奇怪。 Consts.UTF_8 is a Charset whereas HTTP.UTF_8 is a String. Consts.UTF_8是一个字符集,而HTTP.UTF_8是一个字符串。 HttpProtocolParams.setContentCharset(HttpParams httpParams, String charset) is expecting a String, not the Consts Charset. HttpProtocolParams.setContentCharset(HttpParams httpParams,String charset)需要一个String,而不是Consts Charset。

For string we can use by String.valueOf(Consts.UTF_8) 对于字符串,我们可以使用String.valueOf(Consts.UTF_8)

Reference link 参考链接

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

相关问题 Java/Android:如何更新已弃用的 org.apache.http - Java/Android : How to update org.apache.http which is deprecated 在Android 7 org.apache.http(Apache HttpClient 4.0)中启用已弃用的密码套件后获取主机名不匹配 - Getting hostname mismatch after enabling deprecated cipher suite in Android 7 org.apache.http (Apache HttpClient 4.0) org.apache.http库缺少包 - org.apache.http library missing package 我应该在Java应用程序中使用java.net或org.apache.http库来进行HTTP吗? - Should I use the java.net or org.apache.http library for HTTP in my Java application? 在API级别23中删除了org.apache.http包。有什么替代方案? - org.apache.http packages removed in API level 23. What is the alternative? 我在哪里可以下载 org.apache.http 包的 jar 包? - Where can I download the jar for org.apache.http package? Android连接到webservice,org.apache.http贬值了吗? - Android connect to webservice, org.apache.http depricated? 解决“导入org.apache.http包”错误 - Resolve "importing org.apache.http package" errors Maven构建错误:包org.apache.http不存在 - maven build error: package org.apache.http does not exist 为什么这个简单的SOAP客户端不工作(org.apache.http)? - Why is this simple SOAP client not working (org.apache.http)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM