@@ -91,10 +91,10 @@ public abstract class AsyncHttpResponseHandler implements ResponseHandlerInterfa
91
91
92
92
protected static final int BUFFER_SIZE = 4096 ;
93
93
94
- private final Handler handler ;
95
94
public static final String DEFAULT_CHARSET = "UTF-8" ;
96
95
private String responseCharset = DEFAULT_CHARSET ;
97
- private Boolean useSynchronousMode = false ;
96
+ private Handler handler ;
97
+ private boolean useSynchronousMode ;
98
98
99
99
private URI requestURI = null ;
100
100
private Header [] requestHeaders = null ;
@@ -142,6 +142,21 @@ public boolean getUseSynchronousMode() {
142
142
143
143
@ Override
144
144
public void setUseSynchronousMode (boolean value ) {
145
+ // Test that a looper has been prepared prior to setting asynchronous mode.
146
+ if (value && null == Looper .myLooper ()) {
147
+ value = false ;
148
+ Log .i (LOG_TAG , "Current thread has not called Looper.prepare(). Forcing synchronous mode." );
149
+ }
150
+
151
+ // If using asynchronous mode.
152
+ if (value && handler == null ) {
153
+ // Create a handler on current thread to submit tasks
154
+ handler = new ResponderHandler (this );
155
+ } else if (!value && handler != null ) {
156
+ // TODO: Consider adding a flag to remove all queued messages.
157
+ handler = null ;
158
+ }
159
+
145
160
useSynchronousMode = value ;
146
161
}
147
162
@@ -163,19 +178,8 @@ public String getCharset() {
163
178
* Creates a new AsyncHttpResponseHandler
164
179
*/
165
180
public AsyncHttpResponseHandler () {
166
- boolean missingLooper = null == Looper .myLooper ();
167
- // Try to create handler
168
- if (!missingLooper )
169
- handler = new ResponderHandler (this );
170
- else {
171
- // There is no Looper on this thread so synchronous mode should be used.
172
- handler = null ;
173
- setUseSynchronousMode (true );
174
- Log .i (LOG_TAG , "Current thread has not called Looper.prepare(). Forcing synchronous mode." );
175
- }
176
-
177
- // Init Looper by calling postRunnable without an argument.
178
- postRunnable (null );
181
+ // Use asynchronous mode by default.
182
+ setUseSynchronousMode (false );
179
183
}
180
184
181
185
/**
@@ -314,7 +318,7 @@ protected void handleMessage(Message message) {
314
318
}
315
319
316
320
protected void sendMessage (Message msg ) {
317
- if (getUseSynchronousMode ()) {
321
+ if (getUseSynchronousMode () || handler == null ) {
318
322
handleMessage (msg );
319
323
} else if (!Thread .currentThread ().isInterrupted ()) { // do not send messages if request has been cancelled
320
324
handler .sendMessage (msg );
@@ -328,9 +332,9 @@ protected void sendMessage(Message msg) {
328
332
*/
329
333
protected void postRunnable (Runnable runnable ) {
330
334
if (runnable != null ) {
331
- if (getUseSynchronousMode ()) {
332
- // This response handler is synchronous, run on current thread
333
- runnable .run ();
335
+ if (getUseSynchronousMode () || handler == null ) {
336
+ // This response handler is synchronous, run on current thread
337
+ runnable .run ();
334
338
} else {
335
339
// Otherwise, run on provided handler
336
340
handler .post (runnable );
@@ -346,7 +350,15 @@ protected void postRunnable(Runnable runnable) {
346
350
* @return Message instance, should not be null
347
351
*/
348
352
protected Message obtainMessage (int responseMessageId , Object responseMessageData ) {
349
- return Message .obtain (handler , responseMessageId , responseMessageData );
353
+ Message msg ;
354
+ if (handler == null ) {
355
+ msg = Message .obtain ();
356
+ msg .what = responseMessageId ;
357
+ msg .obj = responseMessageData ;
358
+ } else {
359
+ msg = Message .obtain (handler , responseMessageId , responseMessageData );
360
+ }
361
+ return msg ;
350
362
}
351
363
352
364
@ Override
0 commit comments