Skip to content

Commit 2d34e1c

Browse files
committed
1 parent 301cc92 commit 2d34e1c

File tree

5 files changed

+96
-38
lines changed

5 files changed

+96
-38
lines changed

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

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ public class AsyncHttpResponseHandler {
7878

7979
/**
8080
* Sets the charset for the response string. If not set, the default is UTF-8.
81-
* @see <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Charset</a>
8281
*
8382
* @param charset to be used for the response string.
83+
* @see <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html">Charset</a>
8484
*/
8585
public void setCharset(final String charset) {
8686
this.responseCharset = charset;
@@ -168,6 +168,31 @@ public void onFailure(Throwable error, String content) {
168168
onFailure(error);
169169
}
170170

171+
/**
172+
* Fired when a request fails to complete, override to handle in your own code
173+
*
174+
* @param statusCode return HTTP status code
175+
* @param error the underlying cause of the failure
176+
* @param content the response body, if any
177+
*/
178+
public void onFailure(int statusCode, Throwable error, String content) {
179+
// By default, call the chain method onFailure(Throwable,String)
180+
onFailure(error, content);
181+
}
182+
183+
/**
184+
* Fired when a request fails to complete, override to handle in your own code
185+
*
186+
* @param statusCode return HTTP status code
187+
* @param headers return headers, if any
188+
* @param error the underlying cause of the failure
189+
* @param content the response body, if any
190+
*/
191+
public void onFailure(int statusCode, Header[] headers, Throwable error, String content) {
192+
// By default, call the chain method onFailure(int,Throwable,String)
193+
onFailure(statusCode, error, content);
194+
}
195+
171196

172197
//
173198
// Pre-processing of messages (executes in background threadpool thread)
@@ -177,12 +202,12 @@ protected void sendSuccessMessage(int statusCode, Header[] headers, String respo
177202
sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{statusCode, headers, responseBody}));
178203
}
179204

180-
protected void sendFailureMessage(Throwable e, String responseBody) {
181-
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{e, responseBody}));
205+
protected void sendFailureMessage(int statusCode, Header[] headers, Throwable e, String responseBody) {
206+
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{statusCode, headers, e, responseBody}));
182207
}
183208

184-
protected void sendFailureMessage(Throwable e, byte[] responseBody) {
185-
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{e, responseBody}));
209+
protected void sendFailureMessage(int statusCode, Header[] headers, Throwable e, byte[] responseBody) {
210+
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{statusCode, headers, e, responseBody}));
186211
}
187212

188213
protected void sendStartMessage() {
@@ -202,8 +227,8 @@ protected void handleSuccessMessage(int statusCode, Header[] headers, String res
202227
onSuccess(statusCode, headers, responseBody);
203228
}
204229

205-
protected void handleFailureMessage(Throwable e, String responseBody) {
206-
onFailure(e, responseBody);
230+
protected void handleFailureMessage(int statusCode, Header[] headers, Throwable e, String responseBody) {
231+
onFailure(statusCode, headers, e, responseBody);
207232
}
208233

209234

@@ -218,7 +243,7 @@ protected void handleMessage(Message msg) {
218243
break;
219244
case FAILURE_MESSAGE:
220245
response = (Object[]) msg.obj;
221-
handleFailureMessage((Throwable) response[0], (String) response[1]);
246+
handleFailureMessage((Integer) response[0], (Header[]) response[1], (Throwable) response[2], (String) response[3]);
222247
break;
223248
case START_MESSAGE:
224249
onStart();
@@ -238,7 +263,7 @@ protected void sendMessage(Message msg) {
238263
}
239264

240265
protected Message obtainMessage(int responseMessage, Object response) {
241-
Message msg = null;
266+
Message msg;
242267
if (handler != null) {
243268
msg = this.handler.obtainMessage(responseMessage, response);
244269
} else {
@@ -256,19 +281,19 @@ protected void sendResponseMessage(HttpResponse response) {
256281
StatusLine status = response.getStatusLine();
257282
String responseBody = null;
258283
try {
259-
HttpEntity entity = null;
284+
HttpEntity entity;
260285
HttpEntity temp = response.getEntity();
261286
if (temp != null) {
262287
entity = new BufferedHttpEntity(temp);
263288
responseBody = EntityUtils.toString(entity, responseCharset);
264289
}
265290
} catch (IOException e) {
266-
sendFailureMessage(e, (String) null);
291+
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), e, (String) null);
267292
return;
268293
}
269294

270295
if (status.getStatusCode() >= 300) {
271-
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
296+
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
272297
} else {
273298
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
274299
}

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,16 @@ public void onSuccess(int statusCode, byte[] binaryData) {
106106
/**
107107
* Fired when a request fails to complete, override to handle in your own code
108108
*
109+
* @param statusCode response HTTP statuse code
110+
* @param headers response headers, if any
109111
* @param error the underlying cause of the failure
110112
* @param binaryData the response body, if any
111113
* @deprecated
112114
*/
113115
@Deprecated
114-
public void onFailure(Throwable error, byte[] binaryData) {
116+
public void onFailure(int statusCode, Header[] headers, Throwable error, byte[] binaryData) {
115117
// By default, call the deprecated onFailure(Throwable) for compatibility
116-
onFailure(error);
118+
onFailure(statusCode, error, null);
117119
}
118120

119121

@@ -126,8 +128,8 @@ protected void sendSuccessMessage(int statusCode, byte[] responseBody) {
126128
}
127129

128130
@Override
129-
protected void sendFailureMessage(Throwable e, byte[] responseBody) {
130-
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{e, responseBody}));
131+
protected void sendFailureMessage(int statusCode, Header[] headers, Throwable e, byte[] responseBody) {
132+
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{statusCode, headers, e, responseBody}));
131133
}
132134

133135
//
@@ -138,8 +140,8 @@ protected void handleSuccessMessage(int statusCode, byte[] responseBody) {
138140
onSuccess(statusCode, responseBody);
139141
}
140142

141-
protected void handleFailureMessage(Throwable e, byte[] responseBody) {
142-
onFailure(e, responseBody);
143+
protected void handleFailureMessage(int statusCode, Header[] headers, Throwable e, byte[] responseBody) {
144+
onFailure(statusCode, headers, e, responseBody);
143145
}
144146

145147
// Methods which emulate android's Handler and Message methods
@@ -153,7 +155,7 @@ protected void handleMessage(Message msg) {
153155
break;
154156
case FAILURE_MESSAGE:
155157
response = (Object[]) msg.obj;
156-
handleFailureMessage((Throwable) response[0], (byte[]) response[1]);
158+
handleFailureMessage((Integer) response[0], (Header[]) response[1], (Throwable) response[2], (byte[]) response[3]);
157159
break;
158160
default:
159161
super.handleMessage(msg);
@@ -169,7 +171,7 @@ protected void sendResponseMessage(HttpResponse response) {
169171
byte[] responseBody = null;
170172
if (contentTypeHeaders.length != 1) {
171173
//malformed/ambiguous HTTP Header, ABORT!
172-
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"), (String) null);
174+
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"), (String) null);
173175
return;
174176
}
175177
Header contentTypeHeader = contentTypeHeaders[0];
@@ -181,7 +183,7 @@ protected void sendResponseMessage(HttpResponse response) {
181183
}
182184
if (!foundAllowedContentType) {
183185
//Content-Type not in allowed list, ABORT!
184-
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"), (String) null);
186+
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"), (String) null);
185187
return;
186188
}
187189
try {
@@ -192,11 +194,11 @@ protected void sendResponseMessage(HttpResponse response) {
192194
}
193195
responseBody = EntityUtils.toByteArray(entity);
194196
} catch (IOException e) {
195-
sendFailureMessage(e, (byte[]) null);
197+
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), e, (byte[]) null);
196198
}
197199

198200
if (status.getStatusCode() >= 300) {
199-
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
201+
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
200202
} else {
201203
sendSuccessMessage(status.getStatusCode(), responseBody);
202204
}

library/src/com/loopj/android/http/FileAsyncHttpResponseHandler.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.os.Message;
44

5+
import org.apache.http.Header;
56
import org.apache.http.HttpResponse;
67
import org.apache.http.StatusLine;
78
import org.apache.http.client.HttpResponseException;
@@ -29,23 +30,35 @@ public void onSuccess(int statusCode, File file) {
2930
}
3031

3132
public void onFailure(Throwable e, File response) {
33+
// By default call lower chain method
34+
onFailure(e);
35+
}
36+
37+
public void onFailure(int statusCode, Throwable e, File response) {
38+
// By default call lower chain method
39+
onFailure(e, response);
40+
}
41+
42+
public void onFailure(int statusCode, Header[] headers, Throwable e, File response) {
43+
// By default call lower chain method
44+
onFailure(statusCode, e, response);
3245
}
3346

3447

3548
protected void sendSuccessMessage(int statusCode, File file) {
3649
sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{statusCode, file}));
3750
}
3851

39-
protected void sendFailureMessage(Throwable e, File file) {
40-
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{e, file}));
52+
protected void sendFailureMessage(int statusCode, Header[] headers, Throwable e, File file) {
53+
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{statusCode, headers, e, file}));
4154
}
4255

4356
protected void handleSuccessMessage(int statusCode, File responseBody) {
4457
onSuccess(statusCode, responseBody);
4558
}
4659

47-
protected void handleFailureMessage(Throwable e, File responseBody) {
48-
onFailure(e, responseBody);
60+
protected void handleFailureMessage(int statusCode, Header[] headers, Throwable e, File responseBody) {
61+
onFailure(statusCode, headers, e, responseBody);
4962
}
5063

5164
// Methods which emulate android's Handler and Message methods
@@ -58,7 +71,7 @@ protected void handleMessage(Message msg) {
5871
break;
5972
case FAILURE_MESSAGE:
6073
response = (Object[]) msg.obj;
61-
handleFailureMessage((Throwable) response[0], (File) response[1]);
74+
handleFailureMessage((Integer) response[0], (Header[]) response[1], (Throwable) response[2], (File) response[3]);
6275
break;
6376
default:
6477
super.handleMessage(msg);
@@ -84,11 +97,11 @@ protected void sendResponseMessage(HttpResponse response) {
8497
buffer.close();
8598

8699
} catch (IOException e) {
87-
sendFailureMessage(e, this.mFile);
100+
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), e, this.mFile);
88101
}
89102

90103
if (status.getStatusCode() >= 300) {
91-
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), this.mFile);
104+
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), this.mFile);
92105
} else {
93106
sendSuccessMessage(status.getStatusCode(), this.mFile);
94107
}

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,27 @@ public void onSuccess(int statusCode, JSONArray response) {
118118
}
119119

120120
public void onFailure(Throwable e, JSONObject errorResponse) {
121+
onFailure(e);
122+
}
123+
124+
public void onFailure(int statusCode, Throwable e, JSONObject errorResponse) {
125+
onFailure(e, errorResponse);
126+
}
127+
128+
public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject errorResponse) {
129+
onFailure(statusCode, e, errorResponse);
121130
}
122131

123132
public void onFailure(Throwable e, JSONArray errorResponse) {
133+
onFailure(e);
134+
}
135+
136+
public void onFailure(int statusCode, Throwable e, JSONArray errorResponse) {
137+
onFailure(e, errorResponse);
138+
}
139+
140+
public void onFailure(int statusCode, Header[] headers, Throwable e, JSONArray errorResponse) {
141+
onFailure(statusCode, e, errorResponse);
124142
}
125143

126144

@@ -138,7 +156,7 @@ public void run() {
138156
Object jsonResponse = parseResponse(responseBody);
139157
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, headers, jsonResponse}));
140158
} catch (JSONException e) {
141-
sendFailureMessage(e, responseBody);
159+
sendFailureMessage(statusCode, headers, e, responseBody);
142160
}
143161
}
144162
}).start();
@@ -188,27 +206,27 @@ protected Object parseResponse(String responseBody) throws JSONException {
188206
}
189207

190208
@Override
191-
protected void handleFailureMessage(final Throwable e, final String responseBody) {
209+
protected void handleFailureMessage(final int statusCode, final Header[] headers, final Throwable e, final String responseBody) {
192210
new Thread(new Runnable() {
193211
@Override
194212
public void run() {
195213
try {
196214
if (responseBody != null) {
197215
Object jsonResponse = parseResponse(responseBody);
198216
if (jsonResponse instanceof JSONObject) {
199-
onFailure(e, (JSONObject) jsonResponse);
217+
onFailure(statusCode, headers, e, (JSONObject) jsonResponse);
200218
} else if (jsonResponse instanceof JSONArray) {
201-
onFailure(e, (JSONArray) jsonResponse);
219+
onFailure(statusCode, headers, e, (JSONArray) jsonResponse);
202220
} else if (jsonResponse instanceof String) {
203-
onFailure(e, (String) jsonResponse);
221+
onFailure(statusCode, headers, e, (String) jsonResponse);
204222
} else {
205-
onFailure(e, responseBody);
223+
onFailure(statusCode, headers, e, responseBody);
206224
}
207225
} else {
208226
onFailure(e, "");
209227
}
210228
} catch (JSONException ex) {
211-
onFailure(e, responseBody);
229+
onFailure(statusCode, headers, e, responseBody);
212230
}
213231
}
214232
}).start();

library/src/com/loopj/android/http/MySSLSocketFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import javax.net.ssl.X509TrustManager;
1717

1818
/**
19-
* This file is introduced to fix HTTPS Post bug on API < ICS
19+
* This file is introduced to fix HTTPS Post bug on API &lt; ICS
2020
* see http://code.google.com/p/android/issues/detail?id=13117#c14
2121
*/
2222
public class MySSLSocketFactory extends SSLSocketFactory {

0 commit comments

Comments
 (0)