Skip to content

Commit b173af3

Browse files
author
Noor Dawod
committed
Add another constructor for controlling usePoolThread.
No need to construct a looper, or get the current looper, when, in essence, we don't wish to use one at all.
1 parent 76003fd commit b173af3

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

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

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,50 @@ public abstract class AsyncHttpResponseHandler implements ResponseHandlerInterfa
103103
private Header[] requestHeaders = null;
104104
private Looper looper = null;
105105

106+
/**
107+
* Creates a new AsyncHttpResponseHandler
108+
*/
109+
public AsyncHttpResponseHandler() {
110+
this(null);
111+
}
112+
113+
/**
114+
* Creates a new AsyncHttpResponseHandler with a user-supplied looper. If
115+
* the passed looper is null, the looper attached to the current thread will
116+
* be used.
117+
*
118+
* @param looper The looper to work with
119+
*/
120+
public AsyncHttpResponseHandler(Looper looper) {
121+
this.looper = looper == null ? Looper.myLooper() : looper;
122+
123+
// Use asynchronous mode by default.
124+
setUseSynchronousMode(false);
125+
126+
// Do not use the pool's thread to fire callbacks by default.
127+
setUsePoolThread(false);
128+
}
129+
130+
/**
131+
* Creates a new AsyncHttpResponseHandler and decide whether the callbacks
132+
* will be fired on current thread's looper or the pool thread's.
133+
*
134+
* @param usePoolThread Whether to use the pool's thread to fire callbacks
135+
*/
136+
public AsyncHttpResponseHandler(boolean usePoolThread) {
137+
// Whether to use the pool's thread to fire callbacks.
138+
setUsePoolThread(usePoolThread);
139+
140+
// When using the pool's thread, there's no sense in having a looper.
141+
if (!getUsePoolThread()) {
142+
// Use the current thread's looper.
143+
this.looper = Looper.myLooper();
144+
145+
// Use asynchronous mode by default.
146+
setUseSynchronousMode(false);
147+
}
148+
}
149+
106150
@Override
107151
public URI getRequestURI() {
108152
return this.requestURI;
@@ -196,28 +240,6 @@ public String getCharset() {
196240
return this.responseCharset == null ? DEFAULT_CHARSET : this.responseCharset;
197241
}
198242

199-
/**
200-
* Creates a new AsyncHttpResponseHandler
201-
*/
202-
public AsyncHttpResponseHandler() {
203-
this(null);
204-
}
205-
206-
/**
207-
* Creates a new AsyncHttpResponseHandler with a user-supplied looper. If
208-
* the passed looper is null, the looper attached to the current thread will
209-
* be used.
210-
*
211-
* @param looper The looper to work with
212-
*/
213-
public AsyncHttpResponseHandler(Looper looper) {
214-
this.looper = looper == null ? Looper.myLooper() : looper;
215-
// Use asynchronous mode by default.
216-
setUseSynchronousMode(false);
217-
// Do not use the pool's thread to run the handler.
218-
setUsePoolThread(false);
219-
}
220-
221243
/**
222244
* Fired when the request progress, override to handle in your own code
223245
*

0 commit comments

Comments
 (0)