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