Skip to content

Commit 76003fd

Browse files
author
Noor Dawod
committed
Enable responses to be fired on the same thread which was used in the request.
This is handy in situations where more work needs to be done on the thread which doesn't require UI changes, like for example processing JSON responses, parsing images, etc.
1 parent de10c01 commit 76003fd

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpCo
12451245
throw new IllegalArgumentException("ResponseHandler must not be null");
12461246
}
12471247

1248-
if (responseHandler.getUseSynchronousMode()) {
1248+
if (responseHandler.getUseSynchronousMode() && !responseHandler.getUsePoolThread()) {
12491249
throw new IllegalArgumentException("Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
12501250
}
12511251

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public abstract class AsyncHttpResponseHandler implements ResponseHandlerInterfa
9797
private String responseCharset = DEFAULT_CHARSET;
9898
private Handler handler;
9999
private boolean useSynchronousMode;
100+
private boolean usePoolThread;
100101

101102
private URI requestURI = null;
102103
private Header[] requestHeaders = null;
@@ -164,6 +165,23 @@ public void setUseSynchronousMode(boolean sync) {
164165
useSynchronousMode = sync;
165166
}
166167

168+
@Override
169+
public boolean getUsePoolThread() {
170+
return usePoolThread;
171+
}
172+
173+
@Override
174+
public void setUsePoolThread(boolean pool) {
175+
// If pool thread is to be used, there's no point in keeping a reference
176+
// to the looper and no need for a handler.
177+
if (pool) {
178+
looper = null;
179+
handler = null;
180+
}
181+
182+
usePoolThread = pool;
183+
}
184+
167185
/**
168186
* Sets the charset for the response string. If not set, the default is UTF-8.
169187
*
@@ -196,6 +214,8 @@ public AsyncHttpResponseHandler(Looper looper) {
196214
this.looper = looper == null ? Looper.myLooper() : looper;
197215
// Use asynchronous mode by default.
198216
setUseSynchronousMode(false);
217+
// Do not use the pool's thread to run the handler.
218+
setUsePoolThread(false);
199219
}
200220

201221
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void run() {
9797
}
9898
}
9999
};
100-
if (!getUseSynchronousMode()) {
100+
if (!getUseSynchronousMode() && !getUsePoolThread()) {
101101
new Thread(parser).start();
102102
} else {
103103
// In synchronous mode everything should be run on one thread
@@ -133,7 +133,7 @@ public void run() {
133133
}
134134
}
135135
};
136-
if (!getUseSynchronousMode()) {
136+
if (!getUseSynchronousMode() && !getUsePoolThread()) {
137137
new Thread(parser).start();
138138
} else {
139139
// In synchronous mode everything should be run on one thread

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public void run() {
144144
}
145145
}
146146
};
147-
if (!getUseSynchronousMode()) {
147+
if (!getUseSynchronousMode() && !getUsePoolThread()) {
148148
new Thread(parser).start();
149149
} else {
150150
// In synchronous mode everything should be run on one thread
@@ -189,7 +189,7 @@ public void run() {
189189
}
190190
}
191191
};
192-
if (!getUseSynchronousMode()) {
192+
if (!getUseSynchronousMode() && !getUsePoolThread()) {
193193
new Thread(parser).start();
194194
} else {
195195
// In synchronous mode everything should be run on one thread

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ public interface ResponseHandlerInterface {
128128
*/
129129
boolean getUseSynchronousMode();
130130

131+
/**
132+
* Sets whether the handler should be executed on the pool's thread or the
133+
* UI thread
134+
*
135+
* @param usePoolThread if the ResponseHandler should run on pool's thread
136+
*/
137+
void setUsePoolThread(boolean usePoolThread);
138+
139+
/**
140+
* Returns whether the handler should be executed on the pool's thread
141+
* or the UI thread
142+
*
143+
* @return boolean if the ResponseHandler should run on pool's thread
144+
*/
145+
boolean getUsePoolThread();
146+
131147
/**
132148
* This method is called once by the system when the response is about to be
133149
* processed by the system. The library makes sure that a single response

0 commit comments

Comments
 (0)