Android Web服务调用使用哪一个-HttpURLConnection或DefaultHttpClient?

Android Web服务调用使用哪一个-HttpURLConnection或DefaultHttpClient?,android,web-services,httpurlconnection,Android,Web Services,Httpurlconnection,我在我的应用程序中只使用了DefaultHttpClient for WebService调用。 但由于DefaultHttpClient已被弃用,我不知道该使用什么。 我想从中挑选最好的,以便我的进一步发展。 还建议我是否有其他调用Web服务的最佳方式。DefaultHttpClient是一种仍在使用的Apache库,但现在,HttpURLConnection诞生了,谷歌推荐它,因为它比Apache库更适合移动应用程序。DefaultHttpClient也可以在android环境之外使用,但在

我在我的应用程序中只使用了DefaultHttpClient for WebService调用。 但由于DefaultHttpClient已被弃用,我不知道该使用什么。 我想从中挑选最好的,以便我的进一步发展。
还建议我是否有其他调用Web服务的最佳方式。

DefaultHttpClient是一种仍在使用的Apache库,但现在,HttpURLConnection诞生了,谷歌推荐它,因为它比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是一种仍在使用的Apache库,但现在,HttpURLConnection诞生了,谷歌推荐它,因为它比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的官方网络相关优化库。它简化了网络调用的方式,速度更快,而且默认情况下它的操作是异步的(您不需要担心网络调用的自定义线程)

下面是一个使用截击的好教程:


Android截击指南:

DefaultHTTPClient在Android中不受欢迎,在Marshmallow(Android 6.0)及其后的API中不再受支持。HTTPURLConnection优先于HTTPClient

对于网络操作,我建议您使用Volley,Android的官方网络相关优化库。它简化了网络调用的方式,速度更快,而且默认情况下它的操作是异步的(您不需要担心网络调用的自定义线程)

下面是一个使用截击的好教程:


Android截击指南:

因为Android已经弃用了
DefaultHTTPClient
类和方法。
Android 6.0版本取消了对Apache HTTP客户端的支持。如果您的应用程序正在使用此客户端,并且目标是Android 2.3(API级别9)或更高版本,请改用
HttpURLConnection
类。此API效率更高,因为它通过透明压缩和响应缓存减少了网络使用,并将功耗降至最低。
要继续使用Apache HTTP API,必须首先在build.gradle文件中声明以下编译时依赖项:

android {
    useLibrary 'org.apache.http.legacy'
}
这里是一个URL,您可以从中获得有关HttpURLConnection的更多详细信息。
因为Android已经弃用了
DefaultHTTPClient
类和方法。
Android 6.0版本取消了对Apache HTTP客户端的支持。如果您的应用程序正在使用此客户端,并且目标是Android 2.3(API级别9)或更高版本,请改用
HttpURLConnection
类。此API效率更高,因为它通过透明压缩和响应缓存减少了网络使用,并将功耗降至最低。
要继续使用Apache HTTP API,必须首先在build.gradle文件中声明以下编译时依赖项:

android {
    useLibrary 'org.apache.http.legacy'
}
这里是一个URL,您可以从中获得有关HttpURLConnection的更多详细信息。

如果我使用connection.setRequestProperty(“内容类型”,“应用程序/x-www-form-urlencoded;charset=“+charset”);那我就得不到输出了。如果我使用connection.setRequestProperty(“内容类型”,“应用程序/x-www-form-urlencoded;charset=“+charset”);那我就得不到输出了。如果没有此请求,属性将正常工作。谢谢。但如果我们排除网络,则HTTPURLConnection是与服务器通信的更好选项。网络包括一切,与服务器通信,发送POST/GET请求,接收服务器响应,一切都是网络。:)如果您使用Volley,就不必编写大量与服务器通信的代码。谢谢。但如果我们排除网络,则HTTPURLConnection是与服务器通信的更好选项。网络包括一切,与服务器通信,发送POST/GET请求,接收服务器响应,一切都是网络。:)如果您使用截击,就不必编写大量与服务器通信的代码。