Skip to content

Commit 7051307

Browse files
committed
Some basic documentation
1 parent 687ced3 commit 7051307

File tree

4 files changed

+88
-14
lines changed

4 files changed

+88
-14
lines changed

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

examples/ExampleUsage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import com.loopj.android.http.AsyncHttpClient;
2-
import com.loopj.android.http.AsyncHttpRequest;
1+
import com.loopj.android.http.*;
32

4-
public class ExampleUsage {
3+
public class ExampleUsage {
54
public static void makeRequest() {
65
AsyncHttpClient client = new AsyncHttpClient("My User Agent");
6+
77
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
88
@Override
99
public void onSuccess(String response) {
10-
Log.d("ExampleUsage", response);
10+
System.out.println(response);
1111
}
1212
});
1313
}

examples/TwitterRestClient.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import java.util.Locale;
1+
// Static wrapper library around AsyncHttpClient
22

3-
import com.loopj.android.http.AsyncHttpClient;
4-
import com.loopj.android.http.AsyncHttpRequest;
5-
import com.loopj.android.http.AsyncHttpResponseHandler;
6-
import com.loopj.android.http.RequestParams;
3+
import com.loopj.android.http.*;
74

85
public class TwitterRestClient {
96
private static final String USER_AGENT = "Example Twitter Rest Client";

examples/TwitterRestClientUsage.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import org.json.JSONArray;
2-
import org.json.JSONException;
3-
import org.json.JSONObject;
4-
5-
import com.loopj.android.http.JsonHttpResponseHandler;
1+
import org.json.*;
2+
import com.loopj.android.http.*;
63

74
class TwitterRestClientUsage {
85
public void getPublicTimeline() {
@@ -15,6 +12,7 @@ public void onSuccess(Object response) {
1512
JSONObject firstEvent = timeline.get(0);
1613
String tweetText = firstEvent.getString("text");
1714

15+
// Do something with the response
1816
System.out.println(tweetText);
1917
} catch(JSONException e) {
2018
e.printStackTrace();

0 commit comments

Comments
 (0)