|
1 | 1 | Asynchronous Http Client for Android
|
2 | 2 | ====================================
|
3 | 3 |
|
4 |
| -An asynchronous callback-based Http client for Android built on top of Apache's [HttpClient](http://hc.apache.org/httpcomponents-client-ga/) libraries. |
| 4 | +An asynchronous, callback-based Http client for Android built on top of Apache's [HttpClient](http://hc.apache.org/httpcomponents-client-ga/) libraries. |
| 5 | + |
5 | 6 |
|
6 | 7 | Features
|
7 | 8 | --------
|
8 |
| -* Make asynchronous HTTP requests, handle responses in callbacks |
9 |
| -* HTTP requests do not happen in the android UI thread |
10 |
| -* Requests use a threadpool to cap concurrent resource usage |
11 |
| -* Retry requests to help with bad connectivity |
12 |
| -* GET/POST params builder (RequestParams) |
13 |
| -* Optional built-in response parsing into JSON (JsonHttpResponseHandler) |
14 |
| -* Optional persistent cookie store, saves cookies into your app's SharedPreferences |
15 |
| - |
16 |
| -Basic Example |
17 |
| -------------- |
18 |
| - import com.loopj.android.http.*; |
19 |
| - |
20 |
| - public class ExampleUsage { |
21 |
| - public static void makeRequest() { |
22 |
| - AsyncHttpClient client = new AsyncHttpClient("My User Agent"); |
23 |
| - |
24 |
| - client.get("http://www.google.com", new AsyncHttpResponseHandler() { |
25 |
| - @Override |
26 |
| - public void onSuccess(String response) { |
27 |
| - System.out.println(response); |
28 |
| - } |
29 |
| - }); |
30 |
| - } |
31 |
| - } |
32 |
| - |
33 |
| -How to Build a Basic Twitter Rest Client |
34 |
| ----------------------------------------- |
35 |
| - // Build a static wrapper library around AsyncHttpClient |
36 |
| - import com.loopj.android.http.*; |
37 |
| - |
38 |
| - public class TwitterRestClient { |
39 |
| - private static final String USER_AGENT = "Example Twitter Rest Client"; |
40 |
| - private static final String BASE_URL = "http://api.twitter.com/1/"; |
41 |
| - |
42 |
| - private static AsyncHttpClient client = new AsyncHttpClient(USER_AGENT); |
43 |
| - |
44 |
| - public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { |
45 |
| - client.get(getAbsoluteUrl(url), params, responseHandler); |
46 |
| - } |
47 |
| - |
48 |
| - public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { |
49 |
| - client.get(getAbsoluteUrl(url), params, responseHandler); |
50 |
| - } |
51 |
| - |
52 |
| - private static String getAbsoluteUrl(String relativeUrl) { |
53 |
| - return BASE_URL + relativeUrl; |
54 |
| - } |
55 |
| - } |
56 |
| - |
| 9 | +- Make **asynchronous** HTTP requests, handle responses in **anonymous callbacks** |
| 10 | +- HTTP requests happen **outside the UI thread** |
| 11 | +- Requests use a **threadpool** to cap concurrent resource usage |
| 12 | +- GET/POST **params builder** (RequestParams) |
| 13 | +- Automatic smart **request retries** optimized for spotty mobile connections |
| 14 | +- Automatic **gzip** response decoding support for super-fast requests |
| 15 | +- Optional built-in response parsing into **JSON** (JsonHttpResponseHandler) |
| 16 | +- Optional **persistent cookie store**, saves cookies into your app's SharedPreferences |
57 | 17 |
|
58 |
| - // Use the TwitterRestClient in your app |
59 |
| - import org.json.*; |
60 |
| - import com.loopj.android.http.*; |
61 | 18 |
|
62 |
| - class TwitterRestClientUsage { |
63 |
| - public void getPublicTimeline() throws JSONException { |
64 |
| - TwitterRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() { |
65 |
| - @Override |
66 |
| - public void onSuccess(JSONArray response) { |
67 |
| - JSONObject firstEvent = timeline.get(0); |
68 |
| - String tweetText = firstEvent.getString("text"); |
| 19 | +Documentation, Features and Examples |
| 20 | +------------------------------------ |
| 21 | +Full details and documentation can be found on the project page here: |
69 | 22 |
|
70 |
| - // Do something with the response |
71 |
| - System.out.println(tweetText); |
72 |
| - } |
73 |
| - }); |
74 |
| - } |
75 |
| - } |
| 23 | +http://loopj.com/android-async-http/ |
0 commit comments