|
| 1 | +Asynchronous Http Client for Android |
| 2 | +==================================== |
| 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. |
| 5 | + |
| 6 | +Features |
| 7 | +-------- |
| 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 | +* GET/POST params builder (RequestParams) |
| 12 | +* Optional built-in response parsing into JSON (JsonHttpResponseHandler) |
| 13 | +* Optional persistent cookie store, saves cookies into your app's SharedPreferences |
| 14 | + |
| 15 | +Basic Example |
| 16 | +------------- |
| 17 | + import com.loopj.android.http.*; |
| 18 | + |
| 19 | + public class ExampleUsage { |
| 20 | + public static void makeRequest() { |
| 21 | + AsyncHttpClient client = new AsyncHttpClient("My User Agent"); |
| 22 | + |
| 23 | + client.get("http://www.google.com", new AsyncHttpResponseHandler() { |
| 24 | + @Override |
| 25 | + public void onSuccess(String response) { |
| 26 | + System.out.println(response); |
| 27 | + } |
| 28 | + }); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | +How to Build a Basic Twitter Rest Client |
| 33 | +---------------------------------------- |
| 34 | + // Build a static wrapper library around AsyncHttpClient |
| 35 | + import com.loopj.android.http.*; |
| 36 | + |
| 37 | + public class TwitterRestClient { |
| 38 | + private static final String USER_AGENT = "Example Twitter Rest Client"; |
| 39 | + private static final String BASE_URL = "http://api.twitter.com/1/"; |
| 40 | + |
| 41 | + private static AsyncHttpClient client = new AsyncHttpClient(USER_AGENT); |
| 42 | + |
| 43 | + public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { |
| 44 | + client.get(getAbsoluteUrl(url), params, responseHandler); |
| 45 | + } |
| 46 | + |
| 47 | + public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { |
| 48 | + client.get(getAbsoluteUrl(url), params, responseHandler); |
| 49 | + } |
| 50 | + |
| 51 | + private static String getAbsoluteUrl(String relativeUrl) { |
| 52 | + return BASE_URL + relativeUrl; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Use the TwitterRestClient in your app |
| 57 | + import org.json.*; |
| 58 | + import com.loopj.android.http.*; |
| 59 | + |
| 60 | + class TwitterRestClientUsage { |
| 61 | + public void getPublicTimeline() { |
| 62 | + TwitterRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() { |
| 63 | + @Override |
| 64 | + public void onSuccess(Object response) { |
| 65 | + JSONArray timeline = (JSONArray)response; |
| 66 | + |
| 67 | + try { |
| 68 | + JSONObject firstEvent = timeline.get(0); |
| 69 | + String tweetText = firstEvent.getString("text"); |
| 70 | + |
| 71 | + // Do something with the response |
| 72 | + System.out.println(tweetText); |
| 73 | + } catch(JSONException e) { |
| 74 | + e.printStackTrace(); |
| 75 | + } |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
0 commit comments