Skip to content

Commit c7c3f85

Browse files
committed
Merge pull request android-async-http#90 from Zanfa/master
Added passing on the status code to the response handler if the request was successful
2 parents 32ae848 + 8d2ed57 commit c7c3f85

File tree

3 files changed

+76
-34
lines changed

3 files changed

+76
-34
lines changed

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818

1919
package com.loopj.android.http;
2020

21-
import java.io.IOException;
22-
21+
import android.os.Handler;
22+
import android.os.Looper;
23+
import android.os.Message;
2324
import org.apache.http.HttpEntity;
2425
import org.apache.http.HttpResponse;
2526
import org.apache.http.StatusLine;
2627
import org.apache.http.client.HttpResponseException;
2728
import org.apache.http.entity.BufferedHttpEntity;
2829
import org.apache.http.util.EntityUtils;
2930

30-
import android.os.Handler;
31-
import android.os.Message;
32-
import android.os.Looper;
31+
import java.io.IOException;
3332

3433
/**
3534
* Used to intercept and handle the responses from requests made using
@@ -109,6 +108,15 @@ public void onFinish() {}
109108
*/
110109
public void onSuccess(String content) {}
111110

111+
/**
112+
* Fired when a request returns successfully, override to handle in your own code
113+
* @param statusCode the status code of the response
114+
* @param content the body of the HTTP response from the server
115+
*/
116+
public void onSuccess(int statusCode, String content) {
117+
onSuccess(content);
118+
}
119+
112120
/**
113121
* Fired when a request fails to complete, override to handle in your own code
114122
* @param error the underlying cause of the failure
@@ -131,8 +139,8 @@ public void onFailure(Throwable error, String content) {
131139
// Pre-processing of messages (executes in background threadpool thread)
132140
//
133141

134-
protected void sendSuccessMessage(String responseBody) {
135-
sendMessage(obtainMessage(SUCCESS_MESSAGE, responseBody));
142+
protected void sendSuccessMessage(int statusCode, String responseBody) {
143+
sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{new Integer(statusCode), responseBody}));
136144
}
137145

138146
protected void sendFailureMessage(Throwable e, String responseBody) {
@@ -156,8 +164,8 @@ protected void sendFinishMessage() {
156164
// Pre-processing of messages (in original calling thread, typically the UI thread)
157165
//
158166

159-
protected void handleSuccessMessage(String responseBody) {
160-
onSuccess(responseBody);
167+
protected void handleSuccessMessage(int statusCode, String responseBody) {
168+
onSuccess(statusCode, responseBody);
161169
}
162170

163171
protected void handleFailureMessage(Throwable e, String responseBody) {
@@ -168,13 +176,16 @@ protected void handleFailureMessage(Throwable e, String responseBody) {
168176

169177
// Methods which emulate android's Handler and Message methods
170178
protected void handleMessage(Message msg) {
179+
Object[] response;
180+
171181
switch(msg.what) {
172182
case SUCCESS_MESSAGE:
173-
handleSuccessMessage((String)msg.obj);
183+
response = (Object[])msg.obj;
184+
handleSuccessMessage(((Integer) response[0]).intValue(), (String) response[1]);
174185
break;
175186
case FAILURE_MESSAGE:
176-
Object[] repsonse = (Object[])msg.obj;
177-
handleFailureMessage((Throwable)repsonse[0], (String)repsonse[1]);
187+
response = (Object[])msg.obj;
188+
handleFailureMessage((Throwable)response[0], (String)response[1]);
178189
break;
179190
case START_MESSAGE:
180191
onStart();
@@ -205,7 +216,6 @@ protected Message obtainMessage(int responseMessage, Object response) {
205216
return msg;
206217
}
207218

208-
209219
// Interface to AsyncHttpRequest
210220
void sendResponseMessage(HttpResponse response) {
211221
StatusLine status = response.getStatusLine();
@@ -224,7 +234,7 @@ void sendResponseMessage(HttpResponse response) {
224234
if(status.getStatusCode() >= 300) {
225235
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
226236
} else {
227-
sendSuccessMessage(responseBody);
237+
sendSuccessMessage(status.getStatusCode(), responseBody);
228238
}
229239
}
230240
}

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919
package com.loopj.android.http;
2020

21-
import java.io.IOException;
22-
21+
import android.os.Message;
2322
import org.apache.http.Header;
2423
import org.apache.http.HttpEntity;
2524
import org.apache.http.HttpResponse;
@@ -28,9 +27,7 @@
2827
import org.apache.http.entity.BufferedHttpEntity;
2928
import org.apache.http.util.EntityUtils;
3029

31-
import android.os.Handler;
32-
import android.os.Message;
33-
import android.os.Looper;
30+
import java.io.IOException;
3431

3532
/**
3633
* Used to intercept and handle the responses from requests made using
@@ -86,14 +83,23 @@ public BinaryHttpResponseHandler(String[] allowedContentTypes) {
8683

8784
/**
8885
* Fired when a request returns successfully, override to handle in your own code
89-
* @param content the body of the HTTP response from the server
86+
* @param binaryData the body of the HTTP response from the server
9087
*/
9188
public void onSuccess(byte[] binaryData) {}
9289

90+
/**
91+
* Fired when a request returns successfully, override to handle in your own code
92+
* @param statusCode the status code of the response
93+
* @param binaryData the body of the HTTP response from the server
94+
*/
95+
public void onSuccess(int statusCode, byte[] binaryData) {
96+
onSuccess(binaryData);
97+
}
98+
9399
/**
94100
* Fired when a request fails to complete, override to handle in your own code
95101
* @param error the underlying cause of the failure
96-
* @param content the response body, if any
102+
* @param binaryData the response body, if any
97103
*/
98104
public void onFailure(Throwable error, byte[] binaryData) {
99105
// By default, call the deprecated onFailure(Throwable) for compatibility
@@ -105,8 +111,8 @@ public void onFailure(Throwable error, byte[] binaryData) {
105111
// Pre-processing of messages (executes in background threadpool thread)
106112
//
107113

108-
protected void sendSuccessMessage(byte[] responseBody) {
109-
sendMessage(obtainMessage(SUCCESS_MESSAGE, responseBody));
114+
protected void sendSuccessMessage(int statusCode, byte[] responseBody) {
115+
sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{statusCode, responseBody}));
110116
}
111117

112118
protected void sendFailureMessage(Throwable e, byte[] responseBody) {
@@ -117,8 +123,8 @@ protected void sendFailureMessage(Throwable e, byte[] responseBody) {
117123
// Pre-processing of messages (in original calling thread, typically the UI thread)
118124
//
119125

120-
protected void handleSuccessMessage(byte[] responseBody) {
121-
onSuccess(responseBody);
126+
protected void handleSuccessMessage(int statusCode, byte[] responseBody) {
127+
onSuccess(statusCode, responseBody);
122128
}
123129

124130
protected void handleFailureMessage(Throwable e, byte[] responseBody) {
@@ -127,12 +133,14 @@ protected void handleFailureMessage(Throwable e, byte[] responseBody) {
127133

128134
// Methods which emulate android's Handler and Message methods
129135
protected void handleMessage(Message msg) {
136+
Object[] response;
130137
switch(msg.what) {
131138
case SUCCESS_MESSAGE:
132-
handleSuccessMessage((byte[])msg.obj);
139+
response = (Object[])msg.obj;
140+
handleSuccessMessage(((Integer) response[0]).intValue() , (byte[]) response[1]);
133141
break;
134142
case FAILURE_MESSAGE:
135-
Object[] response = (Object[])msg.obj;
143+
response = (Object[])msg.obj;
136144
handleFailureMessage((Throwable)response[0], (byte[])response[1]);
137145
break;
138146
default:
@@ -177,7 +185,7 @@ void sendResponseMessage(HttpResponse response) {
177185
if(status.getStatusCode() >= 300) {
178186
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
179187
} else {
180-
sendSuccessMessage(responseBody);
188+
sendSuccessMessage(status.getStatusCode(), responseBody);
181189
}
182190
}
183191
}

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ public void onSuccess(JSONObject response) {}
6161
*/
6262
public void onSuccess(JSONArray response) {}
6363

64+
/**
65+
* Fired when a request returns successfully and contains a json object
66+
* at the base of the response string. Override to handle in your
67+
* own code.
68+
* @param statusCode the status code of the response
69+
* @param response the parsed json object found in the server response (if any)
70+
*/
71+
public void onSuccess(int statusCode, JSONObject response) {
72+
onSuccess(response);
73+
}
74+
75+
76+
/**
77+
* Fired when a request returns successfully and contains a json array
78+
* at the base of the response string. Override to handle in your
79+
* own code.
80+
* @param statusCode the status code of the response
81+
* @param response the parsed json array found in the server response (if any)
82+
*/
83+
public void onSuccess(int statusCode, JSONArray response) {
84+
onSuccess(response);
85+
}
86+
6487
public void onFailure(Throwable e, JSONObject errorResponse) {}
6588
public void onFailure(Throwable e, JSONArray errorResponse) {}
6689

@@ -70,10 +93,10 @@ public void onFailure(Throwable e, JSONArray errorResponse) {}
7093
//
7194

7295
@Override
73-
protected void sendSuccessMessage(String responseBody) {
96+
protected void sendSuccessMessage(int statusCode, String responseBody) {
7497
try {
7598
Object jsonResponse = parseResponse(responseBody);
76-
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, jsonResponse));
99+
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, jsonResponse}));
77100
} catch(JSONException e) {
78101
sendFailureMessage(e, responseBody);
79102
}
@@ -88,18 +111,19 @@ protected void sendSuccessMessage(String responseBody) {
88111
protected void handleMessage(Message msg) {
89112
switch(msg.what){
90113
case SUCCESS_JSON_MESSAGE:
91-
handleSuccessJsonMessage(msg.obj);
114+
Object[] response = (Object[]) msg.obj;
115+
handleSuccessJsonMessage(((Integer) response[0]).intValue(), response[1]);
92116
break;
93117
default:
94118
super.handleMessage(msg);
95119
}
96120
}
97121

98-
protected void handleSuccessJsonMessage(Object jsonResponse) {
122+
protected void handleSuccessJsonMessage(int statusCode, Object jsonResponse) {
99123
if(jsonResponse instanceof JSONObject) {
100-
onSuccess((JSONObject)jsonResponse);
124+
onSuccess(statusCode, (JSONObject)jsonResponse);
101125
} else if(jsonResponse instanceof JSONArray) {
102-
onSuccess((JSONArray)jsonResponse);
126+
onSuccess(statusCode, (JSONArray)jsonResponse);
103127
} else {
104128
onFailure(new JSONException("Unexpected type " + jsonResponse.getClass().getName()), (JSONObject)null);
105129
}

0 commit comments

Comments
 (0)