Skip to content

Commit 2e220d1

Browse files
committed
Examples and refactor
1 parent 4f5c95e commit 2e220d1

10 files changed

+202
-132
lines changed

examples/ExampleRestClient.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

examples/ExampleUsage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import com.loopj.android.http.AsyncHttpClient;
22
import com.loopj.android.http.AsyncHttpRequest;
33

4-
class ExampleUsage {
4+
public class ExampleUsage {
55
public static void makeRequest() {
66
AsyncHttpClient client = new AsyncHttpClient("My User Agent");
7-
client.get("http://www.google.com/", new AsyncHttpRequest.OnResponseHandler() {
7+
client.get("http://www.google.com/", new AsyncHttpResponseHandler() {
88
@Override
99
public void onSuccess(String response) {
1010
Log.d("ExampleUsage", response);

examples/TwitterRestClient.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Locale;
2+
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;
7+
8+
public class TwitterRestClient {
9+
private static final String USER_AGENT = "Example Twitter Rest Client";
10+
private static final String BASE_URL = "http://api.twitter.com/1/";
11+
12+
private static AsyncHttpClient client = new AsyncHttpClient(USER_AGENT);
13+
14+
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
15+
client.get(getAbsoluteUrl(url), params, responseHandler);
16+
}
17+
18+
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
19+
client.get(getAbsoluteUrl(url), params, responseHandler);
20+
}
21+
22+
private static String getAbsoluteUrl(String relativeUrl) {
23+
return BASE_URL + relativeUrl;
24+
}
25+
}

examples/TwitterRestClientUsage.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import org.json.JSONArray;
2+
import org.json.JSONException;
3+
import org.json.JSONObject;
4+
5+
import com.loopj.android.http.JsonHttpResponseHandler;
6+
7+
class TwitterRestClientUsage {
8+
public void getPublicTimeline() {
9+
ExampleRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() {
10+
@Override
11+
public void onSuccess(Object response) {
12+
JSONArray timeline = (JSONArray)response;
13+
14+
try {
15+
JSONObject firstEvent = timeline.get(0);
16+
String tweetText = firstEvent.getString("text");
17+
18+
System.out.println(tweetText);
19+
} catch(JSONException e) {
20+
e.printStackTrace();
21+
}
22+
}
23+
});
24+
}
25+
}

src/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.apache.http.protocol.HttpContext;
3737

3838
public class AsyncHttpClient {
39-
public static final int DEFAULT_MAX_CONNECTIONS = 4;
39+
public static final int DEFAULT_MAX_CONNECTIONS = 10;
4040
public static final int DEFAULT_SOCKET_TIMEOUT = 30 * 1000;
4141
private static final String ENCODING = "UTF-8";
4242
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
@@ -48,6 +48,7 @@ public class AsyncHttpClient {
4848
private DefaultHttpClient httpClient;
4949
private HttpContext httpContext;
5050
private ExecutorService threadPool;
51+
private PrefetchCache prefetchCache;
5152

5253
public AsyncHttpClient(String userAgent) {
5354
BasicHttpParams httpParams = new BasicHttpParams();
@@ -98,26 +99,30 @@ public void setCookieStore(CookieStore cookieStore) {
9899
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
99100
}
100101

101-
public void get(String url, AsyncHttpRequest.OnResponseHandler responseHandler) {
102+
public void setPrefetchCache(PrefetchCache cache) {
103+
prefetchCache = cache;
104+
}
105+
106+
public void get(String url, AsyncHttpResponseHandler responseHandler) {
102107
get(url, null, responseHandler);
103108
}
104109

105-
public void get(String url, AsyncHttpParams params, AsyncHttpRequest.OnResponseHandler responseHandler) {
110+
public void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
106111
// Build and append query string (utf8 url encoded)
107112
if(params != null) {
108113
String paramString = params.getParamString();
109114
url += "?" + paramString;
110115
}
111116

112117
// Fire up the request in a new thread
113-
executeAsyncRequest(new HttpGet(url), responseHandler);
118+
threadPool.execute(new AsyncHttpRequest(httpClient, httpContext, new HttpGet(url), responseHandler));
114119
}
115120

116-
public void post(String url, AsyncHttpRequest.OnResponseHandler responseHandler) {
121+
public void post(String url, AsyncHttpResponseHandler responseHandler) {
117122
post(url, null, responseHandler);
118123
}
119124

120-
public void post(String url, AsyncHttpParams params, AsyncHttpRequest.OnResponseHandler responseHandler) {
125+
public void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
121126
// Build post object with params
122127
final HttpPost post = new HttpPost(url);
123128
if(params != null) {
@@ -128,11 +133,7 @@ public void post(String url, AsyncHttpParams params, AsyncHttpRequest.OnResponse
128133
}
129134

130135
// Fire up the request in a new thread
131-
executeAsyncRequest(post, responseHandler);
132-
}
133-
134-
private void executeAsyncRequest(final HttpRequestBase request, final AsyncHttpRequest.OnResponseHandler responseHandler) {
135-
threadPool.execute(new AsyncHttpRequest(httpClient, httpContext, request, responseHandler));
136+
threadPool.execute(new AsyncHttpRequest(httpClient, httpContext, post, responseHandler));
136137
}
137138

138139
private static class InflatingEntity extends HttpEntityWrapper {

src/com/loopj/android/http/AsyncHttpRequest.java

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,18 @@
22

33
import java.io.IOException;
44

5-
import org.apache.http.HttpEntity;
65
import org.apache.http.HttpResponse;
7-
import org.apache.http.StatusLine;
86
import org.apache.http.client.HttpClient;
9-
import org.apache.http.client.HttpResponseException;
107
import org.apache.http.client.methods.HttpUriRequest;
11-
import org.apache.http.entity.BufferedHttpEntity;
128
import org.apache.http.protocol.HttpContext;
13-
import org.apache.http.util.EntityUtils;
14-
15-
import android.os.Handler;
16-
import android.os.Message;
17-
import android.os.Looper;
189

1910
public class AsyncHttpRequest implements Runnable {
20-
private static final int RESPONSE_MESSAGE = 0;
21-
private static final int ERROR_MESSAGE = 1;
22-
private static final int START_MESSAGE = 2;
23-
private static final int FINISH_MESSAGE = 3;
24-
2511
private HttpClient client;
2612
private HttpContext context;
2713
private HttpUriRequest request;
28-
private OnResponseHandler responseHandler;
29-
30-
public static class OnResponseHandler extends Handler {
31-
public OnResponseHandler() {
32-
super(Looper.getMainLooper());
33-
}
34-
35-
@Override
36-
public void handleMessage(Message msg) {
37-
switch(msg.what) {
38-
case RESPONSE_MESSAGE:
39-
handleResponseMessage((HttpResponse)msg.obj);
40-
break;
41-
case ERROR_MESSAGE:
42-
handleErrorMessage((Throwable)msg.obj);
43-
break;
44-
case START_MESSAGE:
45-
onStart();
46-
break;
47-
case FINISH_MESSAGE:
48-
onFinish();
49-
break;
50-
}
51-
}
52-
53-
private void handleResponseMessage(HttpResponse response) {
54-
StatusLine status = response.getStatusLine();
55-
if(status.getStatusCode() >= 300) {
56-
onFailure(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
57-
} else {
58-
try {
59-
HttpEntity entity = null;
60-
HttpEntity temp = response.getEntity();
61-
if(temp != null) {
62-
entity = new BufferedHttpEntity(temp);
63-
}
64-
65-
onSuccess(EntityUtils.toString(entity));
66-
} catch(IOException e) {
67-
onFailure(e);
68-
}
69-
}
70-
}
71-
72-
private void handleErrorMessage(Throwable e) {
73-
onFailure(e);
74-
}
75-
76-
// Public callbacks
77-
public void onStart() {}
78-
public void onFinish() {}
79-
public void onSuccess(String content) {}
80-
public void onFailure(Throwable error) {}
81-
}
14+
private AsyncHttpResponseHandler responseHandler;
8215

83-
public AsyncHttpRequest(HttpClient client, HttpContext context, HttpUriRequest request, OnResponseHandler responseHandler) {
16+
public AsyncHttpRequest(HttpClient client, HttpContext context, HttpUriRequest request, AsyncHttpResponseHandler responseHandler) {
8417
this.client = client;
8518
this.context = context;
8619
this.request = request;
@@ -90,18 +23,18 @@ public AsyncHttpRequest(HttpClient client, HttpContext context, HttpUriRequest r
9023
public void run() {
9124
try {
9225
if(responseHandler != null){
93-
responseHandler.sendMessage(responseHandler.obtainMessage(START_MESSAGE));
26+
responseHandler.sendStartMessage();
9427
}
9528

9629
HttpResponse response = client.execute(request, context);
9730
if(responseHandler != null) {
98-
responseHandler.sendMessage(responseHandler.obtainMessage(FINISH_MESSAGE));
99-
responseHandler.sendMessage(responseHandler.obtainMessage(RESPONSE_MESSAGE, response));
31+
responseHandler.sendFinishMessage();
32+
responseHandler.sendResponseMessage(response);
10033
}
10134
} catch (IOException e) {
10235
if(responseHandler != null) {
103-
responseHandler.sendMessage(responseHandler.obtainMessage(FINISH_MESSAGE));
104-
responseHandler.sendMessage(responseHandler.obtainMessage(ERROR_MESSAGE, e));
36+
responseHandler.sendFinishMessage();
37+
responseHandler.sendErrorMessage(e);
10538
}
10639
}
10740
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.loopj.android.http;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.http.HttpEntity;
6+
import org.apache.http.HttpResponse;
7+
import org.apache.http.StatusLine;
8+
import org.apache.http.client.HttpResponseException;
9+
import org.apache.http.entity.BufferedHttpEntity;
10+
import org.apache.http.util.EntityUtils;
11+
12+
import android.os.Handler;
13+
import android.os.Message;
14+
import android.os.Looper;
15+
16+
public class AsyncHttpResponseHandler extends Handler {
17+
private static final int RESPONSE_MESSAGE = 0;
18+
private static final int ERROR_MESSAGE = 1;
19+
private static final int START_MESSAGE = 2;
20+
private static final int FINISH_MESSAGE = 3;
21+
22+
public AsyncHttpResponseHandler() {
23+
super(Looper.getMainLooper());
24+
}
25+
26+
public void sendStartMessage() {
27+
sendMessage(obtainMessage(START_MESSAGE));
28+
}
29+
30+
public void sendFinishMessage() {
31+
sendMessage(obtainMessage(FINISH_MESSAGE));
32+
}
33+
34+
public void sendResponseMessage(HttpResponse response) {
35+
sendMessage(obtainMessage(RESPONSE_MESSAGE, response));
36+
}
37+
38+
public void sendErrorMessage(Throwable e) {
39+
sendMessage(obtainMessage(ERROR_MESSAGE, e));
40+
}
41+
42+
@Override
43+
public void handleMessage(Message msg) {
44+
switch(msg.what) {
45+
case RESPONSE_MESSAGE:
46+
handleResponseMessage((HttpResponse)msg.obj);
47+
break;
48+
case ERROR_MESSAGE:
49+
handleErrorMessage((Throwable)msg.obj);
50+
break;
51+
case START_MESSAGE:
52+
onStart();
53+
break;
54+
case FINISH_MESSAGE:
55+
onFinish();
56+
break;
57+
}
58+
}
59+
60+
protected void handleResponseMessage(HttpResponse response) {
61+
StatusLine status = response.getStatusLine();
62+
if(status.getStatusCode() >= 300) {
63+
onFailure(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
64+
} else {
65+
try {
66+
onSuccess(getResponseBody(response));
67+
} catch(IOException e) {
68+
onFailure(e);
69+
}
70+
}
71+
}
72+
73+
protected void handleErrorMessage(Throwable e) {
74+
onFailure(e);
75+
}
76+
77+
protected String getResponseBody(HttpResponse response) throws IOException {
78+
HttpEntity entity = null;
79+
HttpEntity temp = response.getEntity();
80+
if(temp != null) {
81+
entity = new BufferedHttpEntity(temp);
82+
}
83+
84+
return EntityUtils.toString(entity);
85+
}
86+
87+
// Public callbacks
88+
public void onStart() {}
89+
public void onFinish() {}
90+
public void onSuccess(String content) {}
91+
public void onFailure(Throwable error) {}
92+
}

0 commit comments

Comments
 (0)