[转载]AsyncHttpClient 官网的东西 – forever_crying的专栏 – 博客频道 – CSDN.NET.
verview
An asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries. All requests are made outside of your app’s main UI thread, but any callback logic will be executed on the same thread as the callback was created using Android’s Handler message passing.
Features
- Make asynchronous HTTP requests, handle responses in anonymous callbacks
- HTTP requests happen outside the UI thread
- Requests use a threadpool to cap concurrent resource usage
- GET/POST params builder (RequestParams)
- Multipart file uploads with no additional third party libraries
- Tiny size overhead to your application, only 25kb for everything
- Automatic smart request retries optimized for spotty mobile connections
- Automatic gzip response decoding support for super-fast requests
- Binary file (images etc) downloading with
BinaryHttpResponseHandler
- Built-in response parsing into JSON with
JsonHttpResponseHandler
- Persistent cookie store, saves cookies into your app’s SharedPreferences
Who is Using It?
- Instagram is the #1 photo app on android, with over 10million users
- Heyzap
- Social game discovery app with millions of users
- DoubanFM
- Popular personal online music radio service
- Pose
- Pose is the #1 fashion app for sharing and discovering new styles
- Pocket Salsa
- Pocket Salsa is the easiest way to learn how to dance salsa.
Send me a message on github to let me know if you are using this library in a released android application!
Installation & Basic Usage
Download the latest .jar file from github and place it in your Android app’s libs/
folder.
Import the http package.
Create a new AsyncHttpClient
instance and make a request:
Recommended Usage: Make a Static Http Client
In this example, we’ll make a http client class with static accessors to make it easy to communicate with Twitter’s API.
This then makes it very easy to work with the Twitter API in your code:
Check out the AsyncHttpClient, RequestParams and AsyncHttpResponseHandlerJavadocs for more details.
Persistent Cookie Storage with PersistentCookieStore
This library also includes a PersistentCookieStore
which is an implementation of the Apache HttpClient CookieStore
interface that automatically saves cookies to SharedPreferences
storage on the Android device.
This is extremely useful if you want to use cookies to manage authentication sessions, since the user will remain logged in even after closing and re-opening your app.
First, create an instance of AsyncHttpClient
:
Now set this client’s cookie store to be a new instance of PersistentCookieStore
, constructed with an activity or application context (usually this
will suffice):
Any cookies received from servers will now be stored in the persistent cookie store.
To add your own cookies to the store, simply construct a new cookie and call addCookie
:
See the PersistentCookieStore Javadoc for more information.
Adding GET/POST Parameters with RequestParams
The RequestParams
class is used to add optional GET or POST parameters to your requests.RequestParams
can be built and constructed in various ways:
Create empty RequestParams
and immediately add some parameters:
Create RequestParams
for a single parameter:
Create RequestParams
from an existing Map
of key/value strings:
See the RequestParams Javadoc for more information.
Uploading Files with RequestParams
The RequestParams
class additionally supports multipart file uploads as follows:
Add an InputStream
to the RequestParams
to upload:
Add a File
object to the RequestParams
to upload:
Add a byte array to the RequestParams
to upload:
See the RequestParams Javadoc for more information.
Downloading Binary Data with BinaryHttpResponseHandler
The BinaryHttpResponseHandler
class can be used to fetch binary data such as images and other files. For example:
See the BinaryHttpResponseHandler Javadoc for more information.
Adding HTTP Basic Auth credentials
Some requests may need username/password credentials when dealing with API services that use HTTP Basic Access Authentication requests. You can use the method setBasicAuth()
to provide your credentials.
Set username/password for any host and realm for a particular request. By default the Authentication Scope is for any host, port and realm.
You can also provide a more specific Authentication Scope (recommended)
See the RequestParams Javadoc for more information.
Building from Source
To build a .jar
file from source, first make a clone of the android-async-http github repository. You’ll then need to copy the local.properties.dist
file to local.properties
and edit the sdk.dir
setting to point to where you have the android sdk installed. You can then run:
This will generate a file named android-async-http-version.jar
.
Reporting Bugs or Feature Requests
Please report any bugs or feature requests on the github issues page for this project here:
https://github.com/loopj/android-async-http/issues
Credits & Contributors
- James Smith (http://github.com/loopj)
- Creator and Maintainer
- Micah Fivecoate (http://github.com/m5)
- Major Contributor, including the original
RequestParams
- The Droid Fu Project (https://github.com/kaeppler/droid-fu)
- Inspiration and code for better http retries
- Rafael Sanches (http://blog.rafaelsanches.com)
- Original
SimpleMultipartEntity
code - Anthony Persaud (http://github.com/apersaud)
- Added support for HTTP Basic Authentication requests.
- Linden Darling (http://github.com/coreform)
- Added support for binary/image responses
License
The Android Asynchronous Http Client is released under the Android-friendly Apache License, Version 2.0. Read the full license here:
http://www.apache.org/licenses/LICENSE-2.0
About the Author
James Smith, British entrepreneur and developer based in San Francisco.
I’m the co-founder of Bugsnag with Simon Maynard, and from 2009 to 2012 I led up the product team as CTO of Heyzap.