Skip to content

Commit 1423a6d

Browse files
author
Noor Dawod
committed
Add pre- and post-processing for request and response.
1 parent 2e739df commit 1423a6d

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
/**
3434
* Internal class, representing the HttpRequest, done in asynchronous manner
3535
*/
36-
public class AsyncHttpRequest implements PreProcessInterface, Runnable {
36+
public class AsyncHttpRequest implements Runnable {
3737
private final AbstractHttpClient client;
3838
private final HttpContext context;
3939
private final HttpUriRequest request;
@@ -51,8 +51,26 @@ public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriR
5151
this.responseHandler = responseHandler;
5252
}
5353

54-
@Override
55-
public void onPreProcess() {
54+
/**
55+
* This method is called once by the system when the request is about to be
56+
* processed by the system. The library makes sure that a single request
57+
* is pre-processed only once.
58+
*
59+
* @param request The request to pre-process
60+
*/
61+
public void onPreProcessRequest(AsyncHttpRequest request) {
62+
// default action is to do nothing...
63+
}
64+
65+
/**
66+
* This method is called once by the system when the request has been fully
67+
* sent, handled and finished. The library makes sure that a single request
68+
* is post-processed only once.
69+
*
70+
* @param request The request to post-process
71+
*/
72+
public void onPostProcessRequest(AsyncHttpRequest request) {
73+
// default action is to do nothing...
5674
}
5775

5876
@Override
@@ -64,7 +82,7 @@ public void run() {
6482
// Carry out pre-processing for this request only once.
6583
if (!isRequestPreProcessed) {
6684
isRequestPreProcessed = true;
67-
onPreProcess();
85+
onPreProcessRequest(this);
6886
}
6987

7088
if (isCancelled()) {
@@ -97,13 +115,21 @@ public void run() {
97115
responseHandler.sendFinishMessage();
98116
}
99117

118+
if (isCancelled()) {
119+
return;
120+
}
121+
122+
// Carry out post-processing for this request.
123+
onPostProcessRequest(this);
124+
100125
isFinished = true;
101126
}
102127

103128
private void makeRequest() throws IOException {
104129
if (isCancelled()) {
105130
return;
106131
}
132+
107133
// Fixes #115
108134
if (request.getURI().getScheme() == null) {
109135
// subclass of IOException so processed in the caller
@@ -112,12 +138,27 @@ private void makeRequest() throws IOException {
112138

113139
HttpResponse response = client.execute(request, context);
114140

115-
if (!isCancelled() && responseHandler != null) {
141+
if(isCancelled()) {
142+
return;
143+
}
144+
145+
if (responseHandler != null) {
116146
// Carry out pre-processing for this response.
117-
responseHandler.onPreProcess();
147+
responseHandler.onPreProcessResponse(responseHandler, response);
148+
149+
if(isCancelled()) {
150+
return;
151+
}
118152

119153
// The response is ready, handle it.
120154
responseHandler.sendResponseMessage(response);
155+
156+
if(isCancelled()) {
157+
return;
158+
}
159+
160+
// Carry out post-processing for this response.
161+
responseHandler.onPostProcessResponse(responseHandler, response);
121162
}
122163
}
123164

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,12 @@ public void onFinish() {
219219
}
220220

221221
@Override
222-
public void onPreProcess() {
222+
public void onPreProcessResponse(ResponseHandlerInterface instance, HttpResponse response) {
223+
// default action is to do nothing...
224+
}
225+
226+
@Override
227+
public void onPostProcessResponse(ResponseHandlerInterface instance, HttpResponse response) {
223228
// default action is to do nothing...
224229
}
225230

library/src/main/java/com/loopj/android/http/ResponseHandlerInterface.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* Interface to standardize implementations
2929
*/
30-
public interface ResponseHandlerInterface extends PreProcessInterface {
30+
public interface ResponseHandlerInterface {
3131

3232
/**
3333
* Returns data whether request completed successfully
@@ -127,4 +127,24 @@ public interface ResponseHandlerInterface extends PreProcessInterface {
127127
* @return boolean if the ResponseHandler is running in synchronous mode
128128
*/
129129
boolean getUseSynchronousMode();
130+
131+
/**
132+
* This method is called once by the system when the response is about to be
133+
* processed by the system. The library makes sure that a single response
134+
* is pre-processed only once.
135+
*
136+
* @param instance An instance of this response object
137+
* @param response The response to pre-processed
138+
*/
139+
void onPreProcessResponse(ResponseHandlerInterface instance, HttpResponse response);
140+
141+
/**
142+
* This method is called once by the system when the request has been fully
143+
* sent, handled and finished. The library makes sure that a single response
144+
* is post-processed only once.
145+
*
146+
* @param instance An instance of this response object
147+
* @param response The response to post-process
148+
*/
149+
public void onPostProcessResponse(ResponseHandlerInterface instance, HttpResponse response);
130150
}

0 commit comments

Comments
 (0)