sitelink1 https://android-developers.googleblog.co...ients.html 
sitelink2  
sitelink3  

Most network-connected Android apps will use HTTP to send and receive data. Android includes two HTTP clients: HttpURLConnection and Apache HTTP Client. Both support HTTPS, streaming uploads and downloads, configurable timeouts, IPv6 and connection pooling.

Apache HTTP Client

DefaultHttpClient and its sibling AndroidHttpClient are extensible HTTP clients suitable for web browsers. They have large and flexible APIs. Their implementation is stable and they have few bugs.

But the large size of this API makes it difficult for us to improve it without breaking compatibility. The Android team is not actively working on Apache HTTP Client.

HttpURLConnection

HttpURLConnection is a general-purpose, lightweight HTTP client suitable for most applications. This class has humble beginnings, but its focused API has made it easy for us to improve steadily.

Prior to Froyo, HttpURLConnection had some frustrating bugs. In particular, calling close() on a readable InputStream could poison the connection pool. Work around this by disabling connection pooling:

private void disableConnectionReuseIfNecessary() {
    // HTTP connection reuse which was buggy pre-froyo
    if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {
        System.setProperty("http.keepAlive", "false");
    }
}

In Gingerbread, we added transparent response compression. HttpURLConnection will automatically add this header to outgoing requests, and handle the corresponding response:

Accept-Encoding: gzip

Take advantage of this by configuring your Web server to compress responses for clients that can support it. If response compression is problematic, the class documentation shows how to disable it.

Since HTTP’s Content-Length header returns the compressed size, it is an error to use getContentLength() to size buffers for the uncompressed data. Instead, read bytes from the response until InputStream.read() returns -1.

We also made several improvements to HTTPS in Gingerbread. HttpsURLConnectionattempts to connect with Server Name Indication (SNI) which allows multiple HTTPS hosts to share an IP address. It also enables compression and session tickets. Should the connection fail, it is automatically retried without these features. This makes HttpsURLConnection efficient when connecting to up-to-date servers, without breaking compatibility with older ones.

In Ice Cream Sandwich, we are adding a response cache. With the cache installed, HTTP requests will be satisfied in one of three ways:

  • Fully cached responses are served directly from local storage. Because no network connection needs to be made such responses are available immediately.

  • Conditionally cached responses must have their freshness validated by the webserver. The client sends a request like “Give me /foo.png if it changed since yesterday” and the server replies with either the updated content or a 304 Not Modified status. If the content is unchanged it will not be downloaded!

  • Uncached responses are served from the web. These responses will get stored in the response cache for later.

Use reflection to enable HTTP response caching on devices that support it. This sample code will turn on the response cache on Ice Cream Sandwich without affecting earlier releases:

private void enableHttpResponseCache() {
    try {
        long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
        File httpCacheDir = new File(getCacheDir(), "http");
        Class.forName("android.net.http.HttpResponseCache")
            .getMethod("install", File.class, long.class)
            .invoke(null, httpCacheDir, httpCacheSize);
    } catch (Exception httpResponseCacheNotAvailable) {
    }
}

You should also configure your Web server to set cache headers on its HTTP responses.

Which client is best?

Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.

For Gingerbread and better, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use HttpURLConnection; it is where we will be spending our energy going forward.

번호 제목 글쓴이 날짜 조회 수
28 Android Studio for beginners, Part 3: Build and run the app file 황제낙엽 2018.02.02 81
27 Android Studio for beginners, Part 2: Explore and code the app(2) file 황제낙엽 2018.02.02 17
26 Android Studio for beginners, Part 2: Explore and code the app(1) file 황제낙엽 2018.02.02 36
25 Android Studio for beginners, Part 1: Installation and setup file 황제낙엽 2018.02.02 23
24 SDK와 NDK, 그리고 PDK 황제낙엽 2018.01.30 69
23 Error:Execution failed for task ':app:lintVitalRelease'. 황제낙엽 2018.01.29 783
22 Error:The number of method references in a .dex file cannot exceed 64K. 황제낙엽 2018.01.29 70
21 30일 기한 Google Play 개발자 약관 위반 알림 황제낙엽 2018.01.21 125
20 애니메이션 샘플 예제들 file 황제낙엽 2018.01.08 186
19 ConstraintLayout으로 숫자패드 레이아웃을 file 황제낙엽 2017.11.09 144
18 RecyclerView란? (RecyclerView와 ListView 차이) file 황제낙엽 2017.09.11 153
17 Android - Actionbar에 tab을 추가하고 스와이프 동작으로 화면 전환 구현( ViewPager와 FragmentPagerAdapter 사용) file 황제낙엽 2017.09.11 308
16 HTTP 프로토콜을 이용한 Json Post 보내고 받기 file 황제낙엽 2017.08.03 816
15 HttpURLConnection 에서 세션 유지하기(쿠키 사용) 황제낙엽 2017.08.03 661
14 Android HttpURLConnection클래스로 POST 요청하기 황제낙엽 2017.08.01 65
» Android’s HTTP Clients & Apache HTTP Client 어느것이 성능이 더 좋을까? 황제낙엽 2017.07.31 157
12 프로젝트의 AndroidManifest.xml 파일의 내용 file 황제낙엽 2017.07.25 52
11 구글의 안드로이드 개발자들이 만든 애플리케이션 모음 (Apps for Android ) file 황제낙엽 2017.07.25 56
10 크롬에서 디바이스(스마트폰) 연결 정보 확인 황제낙엽 2017.03.28 215
9 JNI 사용시 Android.mk와 Application.mk에 대해서 황제낙엽 2017.03.28 28