Skip to content

Commit 0ad045f

Browse files
committed
Merge pull request android-async-http#170 from rustybox/response-headers
Response headers
2 parents c8cfff9 + a35e7b0 commit 0ad045f

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ bin/
88
gen/
99
_layouts
1010
.DS_Store
11-
gh-pages
11+
gh-pages

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
package com.loopj.android.http;
2020

21+
import android.os.Handler;
22+
import android.os.Looper;
23+
import android.os.Message;
24+
import org.apache.http.Header;
2125
import java.io.IOException;
22-
2326
import org.apache.http.HttpEntity;
2427
import org.apache.http.HttpResponse;
2528
import org.apache.http.StatusLine;
@@ -110,12 +113,23 @@ public void onFinish() {}
110113
*/
111114
public void onSuccess(String content) {}
112115

116+
/**
117+
* Fired when a request returns successfully, override to handle in your own code
118+
* @param statusCode the status code of the response
119+
* @param headers the headers of the HTTP response
120+
* @param content the body of the HTTP response from the server
121+
*/
122+
public void onSuccess(int statusCode, Header[] headers, String content) {
123+
onSuccess(statusCode, content);
124+
}
125+
113126
/**
114127
* Fired when a request returns successfully, override to handle in your own code
115128
* @param statusCode the status code of the response
116129
* @param content the body of the HTTP response from the server
117130
*/
118-
public void onSuccess(int statusCode, String content) {
131+
public void onSuccess(int statusCode, String content)
132+
{
119133
onSuccess(content);
120134
}
121135

@@ -142,8 +156,8 @@ public void onFailure(Throwable error, String content) {
142156
// Pre-processing of messages (executes in background threadpool thread)
143157
//
144158

145-
protected void sendSuccessMessage(int statusCode, String responseBody) {
146-
sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{new Integer(statusCode), responseBody}));
159+
protected void sendSuccessMessage(int statusCode, Header[] headers, String responseBody) {
160+
sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{new Integer(statusCode), headers, responseBody}));
147161
}
148162

149163
protected void sendFailureMessage(Throwable e, String responseBody) {
@@ -167,8 +181,8 @@ protected void sendFinishMessage() {
167181
// Pre-processing of messages (in original calling thread, typically the UI thread)
168182
//
169183

170-
protected void handleSuccessMessage(int statusCode, String responseBody) {
171-
onSuccess(statusCode, responseBody);
184+
protected void handleSuccessMessage(int statusCode, Header[] headers, String responseBody) {
185+
onSuccess(statusCode, headers, responseBody);
172186
}
173187

174188
protected void handleFailureMessage(Throwable e, String responseBody) {
@@ -184,7 +198,7 @@ protected void handleMessage(Message msg) {
184198
switch(msg.what) {
185199
case SUCCESS_MESSAGE:
186200
response = (Object[])msg.obj;
187-
handleSuccessMessage(((Integer) response[0]).intValue(), (String) response[1]);
201+
handleSuccessMessage(((Integer) response[0]).intValue(), (Header[]) response[1], (String) response[2]);
188202
break;
189203
case FAILURE_MESSAGE:
190204
response = (Object[])msg.obj;
@@ -237,7 +251,7 @@ void sendResponseMessage(HttpResponse response) {
237251
if(status.getStatusCode() >= 300) {
238252
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
239253
} else {
240-
sendSuccessMessage(status.getStatusCode(), responseBody);
254+
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
241255
}
242256
}
243-
}
257+
}

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

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.json.JSONException;
2424
import org.json.JSONObject;
2525
import org.json.JSONTokener;
26-
26+
import org.apache.http.Header;
2727
import android.os.Message;
2828

2929
/**
@@ -62,6 +62,18 @@ public void onSuccess(JSONObject response) {}
6262
*/
6363
public void onSuccess(JSONArray response) {}
6464

65+
/**
66+
* Fired when a request returns successfully and contains a json object
67+
* at the base of the response string. Override to handle in your
68+
* own code.
69+
* @param statusCode the status code of the response
70+
* @param headers the headers of the HTTP response
71+
* @param response the parsed json object found in the server response (if any)
72+
*/
73+
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
74+
onSuccess(statusCode, response);
75+
}
76+
6577
/**
6678
* Fired when a request returns successfully and contains a json object
6779
* at the base of the response string. Override to handle in your
@@ -73,6 +85,17 @@ public void onSuccess(int statusCode, JSONObject response) {
7385
onSuccess(response);
7486
}
7587

88+
/**
89+
* Fired when a request returns successfully and contains a json array
90+
* at the base of the response string. Override to handle in your
91+
* own code.
92+
* @param statusCode the status code of the response
93+
* @param headers the headers of the HTTP response
94+
* @param response the parsed json array found in the server response (if any)
95+
*/
96+
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
97+
onSuccess(statusCode, response);
98+
}
7699

77100
/**
78101
* Fired when a request returns successfully and contains a json array
@@ -81,7 +104,7 @@ public void onSuccess(int statusCode, JSONObject response) {
81104
* @param statusCode the status code of the response
82105
* @param response the parsed json array found in the server response (if any)
83106
*/
84-
public void onSuccess(int statusCode, JSONArray response) {
107+
public void onSuccess(int statusCode, JSONArray response) {
85108
onSuccess(response);
86109
}
87110

@@ -94,16 +117,16 @@ public void onFailure(Throwable e, JSONArray errorResponse) {}
94117
//
95118

96119
@Override
97-
protected void sendSuccessMessage(int statusCode, String responseBody) {
98-
if (statusCode != HttpStatus.SC_NO_CONTENT){
99-
try {
100-
Object jsonResponse = parseResponse(responseBody);
101-
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, jsonResponse}));
102-
} catch(JSONException e) {
103-
sendFailureMessage(e, responseBody);
104-
}
105-
}else{
106-
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, new JSONObject()}));
120+
protected void sendSuccessMessage(int statusCode, Header[] headers, String responseBody) {
121+
if (statusCode != HttpStatus.SC_NO_CONTENT){
122+
try {
123+
Object jsonResponse = parseResponse(responseBody);
124+
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, headers, jsonResponse}));
125+
} catch(JSONException e) {
126+
sendFailureMessage(e, responseBody);
127+
}
128+
} else {
129+
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, new JSONObject()}));
107130
}
108131
}
109132

@@ -117,18 +140,18 @@ protected void handleMessage(Message msg) {
117140
switch(msg.what){
118141
case SUCCESS_JSON_MESSAGE:
119142
Object[] response = (Object[]) msg.obj;
120-
handleSuccessJsonMessage(((Integer) response[0]).intValue(), response[1]);
143+
handleSuccessJsonMessage(((Integer) response[0]).intValue(),(Header[]) response[1] ,response[2]);
121144
break;
122145
default:
123146
super.handleMessage(msg);
124147
}
125148
}
126149

127-
protected void handleSuccessJsonMessage(int statusCode, Object jsonResponse) {
150+
protected void handleSuccessJsonMessage(int statusCode,Header[] headers, Object jsonResponse) {
128151
if(jsonResponse instanceof JSONObject) {
129-
onSuccess(statusCode, (JSONObject)jsonResponse);
152+
onSuccess(statusCode, headers, (JSONObject)jsonResponse);
130153
} else if(jsonResponse instanceof JSONArray) {
131-
onSuccess(statusCode, (JSONArray)jsonResponse);
154+
onSuccess(statusCode, headers, (JSONArray)jsonResponse);
132155
} else {
133156
onFailure(new JSONException("Unexpected type " + jsonResponse.getClass().getName()), (JSONObject)null);
134157
}

0 commit comments

Comments
 (0)