哪一个用于 WebService 调用 - HttpURLConnection 或 DefaultHttpClient?
Which one to use for WebService call- HttpURLConnection or DefaultHttpClient?
我在我的应用程序中仅使用 DefaultHttpClient 进行 WebService 调用。
但是由于 DefaultHttpClient 已被弃用,我对使用什么感到困惑。
为了我的进一步发展,我想从中选择最好的。
如果有任何其他调用 WebServices 的最佳方式,也建议我。
DefaultHttpClient 是一种仍在使用的 Apache 库,但如今,HttpURLConnection 诞生了,google 推荐它,因为它比 Apache 库更适合移动应用程序。 DefaultHttpClient 也可以在 android 环境之外使用,但在 android 中,它已被弃用,我们应该使用 HttpUrlConnection 有很多优点:有利于限制 android 内存,有利于电池...
用起来会觉得不是太难,下面是一些有用的代码
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(CONNECT_TIME_OUT);
connection.setReadTimeout(READ_TIME_OUT);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + CHARSET);
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParams.getBytes().length));
if (headers != null) addHeaderFields(connection, headers);
if (!TextUtils.isEmpty(urlParams)) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), CHARSET), true);
writer.append(urlParams);
writer.flush();
writer.close();
}
StringBuilder response = new StringBuilder();
// checks server's status code first
int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
connection.disconnect();
} else {
throw new ApiException(ApiException.APP_EXCEPTION, "Server returned non-OK status: " + status);
}
return response.toString();
DefaultHTTPClient 在 android 中已弃用,Marshmallow (Android 6.0) 及其之后的 API 将不再支持。 HTTPURLConnection 优于 HTTPClient。
对于网络操作,我建议您使用 Volley ,Android 官方网络相关的优化库。它简化了网络调用的方式,速度更快,而且默认情况下它的操作是异步的(您无需担心网络调用的自定义线程)。
这里有一个很好的使用 Volley 的教程:Android working with Volley Library
Android Volley 指南:使用 Volley 传输网络数据
因为 Android 已弃用 DefaultHTTPClient
Class 和方法。
Android 6.0 版删除了对 Apache HTTP 客户端的支持。如果您的应用正在使用此客户端并面向 Android 2.3(API 级别 9)或更高版本,请改用 HttpURLConnection
class。此 API 效率更高,因为它通过透明压缩和响应缓存减少了网络使用,并最大限度地降低了功耗。
要继续使用 Apache HTTP APIs,您必须首先在 build.gradle 文件中声明以下编译时依赖项:
android {
useLibrary 'org.apache.http.legacy'
}
这里是 URL,您可以从中获得有关 HttpURLConnection 的更多详细信息。
https://developer.android.com/reference/java/net/HttpURLConnection.html
我在我的应用程序中仅使用 DefaultHttpClient 进行 WebService 调用。 但是由于 DefaultHttpClient 已被弃用,我对使用什么感到困惑。 为了我的进一步发展,我想从中选择最好的。 如果有任何其他调用 WebServices 的最佳方式,也建议我。
DefaultHttpClient 是一种仍在使用的 Apache 库,但如今,HttpURLConnection 诞生了,google 推荐它,因为它比 Apache 库更适合移动应用程序。 DefaultHttpClient 也可以在 android 环境之外使用,但在 android 中,它已被弃用,我们应该使用 HttpUrlConnection 有很多优点:有利于限制 android 内存,有利于电池... 用起来会觉得不是太难,下面是一些有用的代码
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(CONNECT_TIME_OUT);
connection.setReadTimeout(READ_TIME_OUT);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + CHARSET);
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParams.getBytes().length));
if (headers != null) addHeaderFields(connection, headers);
if (!TextUtils.isEmpty(urlParams)) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), CHARSET), true);
writer.append(urlParams);
writer.flush();
writer.close();
}
StringBuilder response = new StringBuilder();
// checks server's status code first
int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
connection.disconnect();
} else {
throw new ApiException(ApiException.APP_EXCEPTION, "Server returned non-OK status: " + status);
}
return response.toString();
DefaultHTTPClient 在 android 中已弃用,Marshmallow (Android 6.0) 及其之后的 API 将不再支持。 HTTPURLConnection 优于 HTTPClient。
对于网络操作,我建议您使用 Volley ,Android 官方网络相关的优化库。它简化了网络调用的方式,速度更快,而且默认情况下它的操作是异步的(您无需担心网络调用的自定义线程)。
这里有一个很好的使用 Volley 的教程:Android working with Volley Library
Android Volley 指南:使用 Volley 传输网络数据
因为 Android 已弃用 DefaultHTTPClient
Class 和方法。
Android 6.0 版删除了对 Apache HTTP 客户端的支持。如果您的应用正在使用此客户端并面向 Android 2.3(API 级别 9)或更高版本,请改用 HttpURLConnection
class。此 API 效率更高,因为它通过透明压缩和响应缓存减少了网络使用,并最大限度地降低了功耗。
要继续使用 Apache HTTP APIs,您必须首先在 build.gradle 文件中声明以下编译时依赖项:
android {
useLibrary 'org.apache.http.legacy'
}
这里是 URL,您可以从中获得有关 HttpURLConnection 的更多详细信息。 https://developer.android.com/reference/java/net/HttpURLConnection.html