Skip to content

Commit ad6954f

Browse files
committed
Rename handler to BinaryHttpResponseHandler
1 parent 046af5e commit ad6954f

File tree

7 files changed

+76
-104
lines changed

7 files changed

+76
-104
lines changed

AndroidManifest.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.loopj.android.http"
4-
android:versionName="1.3.2"
5-
android:versionCode="1">
6-
<application
7-
android:name="android_async_http">
8-
</application>
9-
<uses-sdk android:minSdkVersion="3" />
10-
<uses-permission android:name="android.permission.INTERNET" />
3+
package="com.loopj.android.http"
4+
android:versionName="1.3.2"
5+
android:versionCode="1">
6+
<application
7+
android:name="android_async_http">
8+
</application>
9+
<uses-sdk android:minSdkVersion="3" />
10+
<uses-permission android:name="android.permission.INTERNET" />
1111
</manifest>

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ class AsyncHttpRequest implements Runnable {
3232
private final HttpContext context;
3333
private final HttpUriRequest request;
3434
private final AsyncHttpResponseHandler responseHandler;
35-
private boolean isImageResponseHandler;
35+
private boolean isBinaryRequest;
3636
private int executionCount;
3737

3838
public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, AsyncHttpResponseHandler responseHandler) {
3939
this.client = client;
4040
this.context = context;
4141
this.request = request;
4242
this.responseHandler = responseHandler;
43-
if(responseHandler instanceof ImageHttpResponseHandler) {
44-
this.isImageResponseHandler = true;
43+
if(responseHandler instanceof BinaryHttpResponseHandler) {
44+
this.isBinaryRequest = true;
4545
}
4646
}
4747

@@ -59,26 +59,26 @@ public void run() {
5959
} catch (IOException e) {
6060
if(responseHandler != null) {
6161
responseHandler.sendFinishMessage();
62-
if(this.isImageResponseHandler) {
63-
responseHandler.sendFailureMessage(e, (byte[]) null);
62+
if(this.isBinaryRequest) {
63+
responseHandler.sendFailureMessage(e, (byte[]) null);
6464
} else {
65-
responseHandler.sendFailureMessage(e, (String) null);
65+
responseHandler.sendFailureMessage(e, (String) null);
6666
}
6767
}
6868
}
6969
}
70-
70+
7171
private void makeRequest() throws IOException {
72-
if(!Thread.currentThread().isInterrupted()) {
73-
HttpResponse response = client.execute(request, context);
74-
if(!Thread.currentThread().isInterrupted()) {
75-
if(responseHandler != null) {
76-
responseHandler.sendResponseMessage(response);
77-
}
78-
} else{
79-
//TODO: should raise InterruptedException? this block is reached whenever the request is cancelled before its response is received
80-
}
81-
}
72+
if(!Thread.currentThread().isInterrupted()) {
73+
HttpResponse response = client.execute(request, context);
74+
if(!Thread.currentThread().isInterrupted()) {
75+
if(responseHandler != null) {
76+
responseHandler.sendResponseMessage(response);
77+
}
78+
} else{
79+
//TODO: should raise InterruptedException? this block is reached whenever the request is cancelled before its response is received
80+
}
81+
}
8282
}
8383

8484
private void makeRequestWithRetries() throws ConnectException {
@@ -100,7 +100,7 @@ private void makeRequestWithRetries() throws ConnectException {
100100
// http://code.google.com/p/android/issues/detail?id=5255
101101
cause = new IOException("NPE in HttpClient" + e.getMessage());
102102
retry = retryHandler.retryRequest(cause, ++executionCount, context);
103-
}
103+
}
104104
}
105105

106106
// no retries left, crap out with exception

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@
6767
* </pre>
6868
*/
6969
public class AsyncHttpResponseHandler {
70-
private static final int SUCCESS_MESSAGE = 0;
71-
private static final int FAILURE_MESSAGE = 1;
72-
private static final int START_MESSAGE = 2;
73-
private static final int FINISH_MESSAGE = 3;
70+
protected static final int SUCCESS_MESSAGE = 0;
71+
protected static final int FAILURE_MESSAGE = 1;
72+
protected static final int START_MESSAGE = 2;
73+
protected static final int FINISH_MESSAGE = 3;
7474

7575
private Handler handler;
7676

src/com/loopj/android/http/ImageHttpResponseHandler.java renamed to src/com/loopj/android/http/BinaryHttpResponseHandler.java

Lines changed: 37 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -33,66 +33,50 @@
3333
import android.os.Looper;
3434

3535
/**
36-
* Used to intercept and handle the responses from requests made using
37-
* {@link AsyncHttpClient}, with automatic handling of png/jpg image data
38-
* (e.g. checks Content-Type against allowed list, Content-length). Receives response body as
39-
* byte array.
40-
* <p>
41-
* Additionally, you can override the {@link #onFailure(Throwable, String)},
42-
* {@link #onStart()}, and {@link #onFinish()} methods as required.
36+
* Used to intercept and handle the responses from requests made using
37+
* {@link AsyncHttpClient}. Receives response body as byte array with a
38+
* content-type whitelist. (e.g. checks Content-Type against allowed list,
39+
* Content-length).
4340
* <p>
4441
* For example:
4542
* <p>
4643
* <pre>
4744
* AsyncHttpClient client = new AsyncHttpClient();
48-
* client.get("http://www.google.com", new AsyncHttpResponseHandler() {
45+
* String[] allowedTypes = new String[] { "image/png" };
46+
* client.get("http://www.example.com/image.png", new BinaryHttpResponseHandler(allowedTypes) {
4947
* &#064;Override
50-
* public void onStart() {
51-
* // Initiated the request
52-
* }
53-
*
54-
* &#064;Override
55-
* public void onSuccess(String response) {
48+
* public void onSuccess(byte[] imageData) {
5649
* // Successfully got a response
5750
* }
58-
*
59-
* &#064;Override
60-
* public void onFailure(Throwable e, String response) {
61-
* // Response failed :(
62-
* }
6351
*
6452
* &#064;Override
65-
* public void onFinish() {
66-
* // Completed the request (either success or failure)
53+
* public void onFailure(Throwable e, byte[] imageData) {
54+
* // Response failed :(
6755
* }
6856
* });
6957
* </pre>
7058
*/
71-
public class ImageHttpResponseHandler extends AsyncHttpResponseHandler {
72-
private static final int SUCCESS_MESSAGE = 0;
73-
private static final int FAILURE_MESSAGE = 1;
74-
private static final int START_MESSAGE = 2;
75-
private static final int FINISH_MESSAGE = 3;
76-
77-
private static String[] mAllowedContentTypes = new String[] { "image/jpeg",
78-
"image/png" };
79-
80-
private Handler handler;
59+
public class BinaryHttpResponseHandler extends AsyncHttpResponseHandler {
60+
// Allow images by default
61+
private static String[] mAllowedContentTypes = new String[] {
62+
"image/jpeg",
63+
"image/png"
64+
};
8165

8266
/**
83-
* Creates a new AsyncHttpResponseHandler
67+
* Creates a new BinaryHttpResponseHandler
8468
*/
85-
public ImageHttpResponseHandler() {
69+
public BinaryHttpResponseHandler() {
8670
super();
8771
}
88-
72+
8973
/**
90-
* Creates a new AsyncHttpResponseHandler, and overrides the default allowed image
91-
* content types with passed String array (hopefully) of image content types.
74+
* Creates a new BinaryHttpResponseHandler, and overrides the default allowed
75+
* content types with passed String array (hopefully) of content types.
9276
*/
93-
public ImageHttpResponseHandler(String[] allowedContentTypes) {
94-
this();
95-
mAllowedContentTypes = allowedContentTypes;
77+
public BinaryHttpResponseHandler(String[] allowedContentTypes) {
78+
this();
79+
mAllowedContentTypes = allowedContentTypes;
9680
}
9781

9882

@@ -104,21 +88,14 @@ public ImageHttpResponseHandler(String[] allowedContentTypes) {
10488
* Fired when a request returns successfully, override to handle in your own code
10589
* @param content the body of the HTTP response from the server
10690
*/
107-
public void onSuccess(byte[] imageData) {}
108-
109-
/**
110-
* Fired when a request fails to complete, override to handle in your own code
111-
* @param error the underlying cause of the failure
112-
* @deprecated use {@link #onFailure(Throwable, String)}
113-
*/
114-
public void onFailure(Throwable error) {}
91+
public void onSuccess(byte[] binaryData) {}
11592

11693
/**
11794
* Fired when a request fails to complete, override to handle in your own code
11895
* @param error the underlying cause of the failure
11996
* @param content the response body, if any
12097
*/
121-
public void onFailure(Throwable error, byte[] imageData) {
98+
public void onFailure(Throwable error, byte[] binaryData) {
12299
// By default, call the deprecated onFailure(Throwable) for compatibility
123100
onFailure(error);
124101
}
@@ -127,7 +104,7 @@ public void onFailure(Throwable error, byte[] imageData) {
127104
//
128105
// Pre-processing of messages (executes in background threadpool thread)
129106
//
130-
107+
131108
protected void sendSuccessMessage(byte[] responseBody) {
132109
sendMessage(obtainMessage(SUCCESS_MESSAGE, responseBody));
133110
}
@@ -148,8 +125,6 @@ protected void handleFailureMessage(Throwable e, byte[] responseBody) {
148125
onFailure(e, responseBody);
149126
}
150127

151-
152-
153128
// Methods which emulate android's Handler and Message methods
154129
protected void handleMessage(Message msg) {
155130
switch(msg.what) {
@@ -160,11 +135,8 @@ protected void handleMessage(Message msg) {
160135
Object[] response = (Object[])msg.obj;
161136
handleFailureMessage((Throwable)response[0], (byte[])response[1]);
162137
break;
163-
case START_MESSAGE:
164-
onStart();
165-
break;
166-
case FINISH_MESSAGE:
167-
onFinish();
138+
default:
139+
super.handleMessage(msg);
168140
break;
169141
}
170142
}
@@ -175,21 +147,21 @@ void sendResponseMessage(HttpResponse response) {
175147
Header[] contentTypeHeaders = response.getHeaders("Content-Type");
176148
byte[] responseBody = null;
177149
if(contentTypeHeaders.length != 1) {
178-
//malformed/ambiguous HTTP Header, ABORT!
179-
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"), responseBody);
180-
return;
150+
//malformed/ambiguous HTTP Header, ABORT!
151+
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"), responseBody);
152+
return;
181153
}
182154
Header contentTypeHeader = contentTypeHeaders[0];
183155
boolean foundAllowedContentType = false;
184156
for(String anAllowedContentType : mAllowedContentTypes) {
185-
if(anAllowedContentType.equals(contentTypeHeader.getValue())) {
186-
foundAllowedContentType = true;
187-
}
157+
if(anAllowedContentType.equals(contentTypeHeader.getValue())) {
158+
foundAllowedContentType = true;
159+
}
188160
}
189161
if(!foundAllowedContentType) {
190-
//Content-Type not in allowed list, ABORT!
191-
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"), responseBody);
192-
return;
162+
//Content-Type not in allowed list, ABORT!
163+
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"), responseBody);
164+
return;
193165
}
194166
try {
195167
HttpEntity entity = null;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
import org.json.JSONTokener;
2525

2626
/**
27-
* Used to intercept and handle the responses from requests made using
27+
* Used to intercept and handle the responses from requests made using
2828
* {@link AsyncHttpClient}, with automatic parsing into a {@link JSONObject}
2929
* or {@link JSONArray}.
3030
* <p>
3131
* This class is designed to be passed to get, post, put and delete requests
3232
* with the {@link #onSuccess(JSONObject)} or {@link #onSuccess(JSONArray)}
3333
* methods anonymously overridden.
3434
* <p>
35-
* Additionally, you can override the other event methods from the
35+
* Additionally, you can override the other event methods from the
3636
* parent class.
3737
*/
3838
public class JsonHttpResponseHandler extends AsyncHttpResponseHandler {
@@ -86,8 +86,8 @@ protected Object parseResponse(String responseBody) throws JSONException {
8686
*/
8787
public void onFailure(Throwable e, JSONObject errorResponse) {}
8888
public void onFailure(Throwable e, JSONArray errorResponse) {}
89-
90-
@Override
89+
90+
@Override
9191
protected void handleFailureMessage(Throwable e, String responseBody) {
9292
super.handleFailureMessage(e, responseBody);
9393
if (responseBody != null) try {
@@ -97,12 +97,12 @@ protected void handleFailureMessage(Throwable e, String responseBody) {
9797
} else if(jsonResponse instanceof JSONArray) {
9898
onFailure(e, (JSONArray)jsonResponse);
9999
}
100-
}
100+
}
101101
catch(JSONException ex) {
102102
onFailure(e, responseBody);
103103
}
104104
else {
105-
onFailure(e, "");
105+
onFailure(e, "");
106106
}
107107
}
108108
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public String toString() {
174174

175175
return result.toString();
176176
}
177-
177+
178178
/**
179179
* Returns an HttpEntity containing all request parameters
180180
*/
@@ -195,7 +195,7 @@ public HttpEntity getEntity() {
195195
for(ConcurrentHashMap.Entry<String, FileWrapper> entry : fileParams.entrySet()) {
196196
FileWrapper file = entry.getValue();
197197
if(file.inputStream != null) {
198-
boolean isLast = currentIndex == lastIndex;
198+
boolean isLast = currentIndex == lastIndex;
199199
if(file.contentType != null) {
200200
multipartEntity.addPart(entry.getKey(), file.getFileName(), file.inputStream, file.contentType, isLast);
201201
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void addPart(final String key, final String fileName, final InputStream f
111111
out.write(tmp, 0, l);
112112
}
113113
if(!isLast)
114-
out.write(("\r\n--" + boundary + "\r\n").getBytes());
114+
out.write(("\r\n--" + boundary + "\r\n").getBytes());
115115
out.flush();
116116
} catch (final IOException e) {
117117
e.printStackTrace();

0 commit comments

Comments
 (0)