2012年4月21日 星期六

[AndroidNet] HTTP API

Android includes two HTTP clients: HttpURLConnection and Apache HTTP Client. Both support HTTPS, streaming uploads and downloads, configurable timeouts, IPv6 and connection pooling.

  • android.net.http.AndroidHttpClient - API 8 - 主要使用 Apache 的 HttpClient
    • Implementation of the Apache DefaultHttpClient that is configured with reasonable default settings and registered schemes for Android, and also lets the user add HttpRequestInterceptor classes. 
    • Don't create this directly, use the newInstance(String) factory method.

Their implementation is stable and they have few bugs. But Android Team is not actively working on Apache HTTP Client.


  • abstract java.net.URLConnection
    • Instances of URLConnection are not reusable: you must use a different instance for each connection to a resource.
    • An URLConnection for HTTP (RFC 2616) used to send and receive data over the web. Data may be of any type and length.
  • java.net.HttpURLConnection extends URLConnection
    • This class may be used to send and receive streaming data whose length is not known in advance.
    • Is a general-purpose, lightweight HTTP client suitable for most applications.
    • Prior to Froyo, HttpURLConnection had some frustrating bugs.
    • Work around this by disabling connection pooling (Refer:  Android’s HTTP Clients | Android Developers Blog)
    • In Gingerbread, we added transparent response compression.
    • In Ice Cream Sandwich, we are adding a response cache.

  • How to choose?
    • Eclair and Froyo  - Apache HTTP client that has fewer bugs.
    • For Gingerbread and better -  HttpURLConnection.
      • 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.
      • Android Team will be spending our energy going forward.

* Reference
- Android’s HTTP Clients | Android Developers Blog
- Android 上的 HTTP 服務相關函式 (I)
- Android 上的 HTTP 服務相關函式 (II)
- Android 上的 HTTP 服務相關函式 (III)

沒有留言:

張貼留言