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.

번호 제목 글쓴이 날짜 조회 수
128 PC를 위한 최고의 안드로이드 에뮬레이터 황제낙엽 2023.01.18 3132
127 [Jetpack Compose UI / 상추님] GDG Jetpack compose 코드랩 수료 후기 (feat. 굿즈) 황제낙엽 2022.12.24 2370
126 [Jetpack Compose UI / kyj1991719] compose - Compose 이해 황제낙엽 2022.12.24 4025
125 [Jetpack Compose UI / 꾸준] Compose 가 뭔데...? 황제낙엽 2022.12.24 4035
124 [Jetpack Compose UI / IIVIKI] Compose 학습과 분석 황제낙엽 2022.12.24 4050
123 [Jetpack Compose UI] 스터디 시작하기 황제낙엽 2022.12.24 4001
122 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 바텀네비게이션 뷰, 네비게이션 AAC file 황제낙엽 2022.12.22 3882
121 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 뷰바인딩 file 황제낙엽 2022.12.22 3850
120 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 라이브데이터 뷰모델 file 황제낙엽 2022.12.21 3826
119 [대왕놀] ImageButton in Compose (IconButton) 황제낙엽 2022.12.20 4105
118 [대왕놀] compose instrumentation test tutorial 황제낙엽 2022.12.20 3985
117 [대왕놀] Switch in Compose 황제낙엽 2022.12.20 4106
116 [대왕놀] EditText in Compose (TextField) 황제낙엽 2022.12.20 4021
115 [대왕놀] margin in Compose 황제낙엽 2022.12.20 4105
114 [대왕놀] modifier in compose 황제낙엽 2022.12.20 4180
113 [대왕놀] Button in Compose file 황제낙엽 2022.12.20 4224
112 [대왕놀] TextView in Compose (Text) 황제낙엽 2022.12.20 4083
111 [대왕놀] ImageView in Compose (Image) 황제낙엽 2022.12.20 4017
110 [대왕놀] LinearLayout in Compose (Row, Column) 황제낙엽 2022.12.20 4113
109 [대왕놀] Introduction to Jetpack compose 황제낙엽 2022.12.20 3964