19
19
package com .loopj .android .http ;
20
20
21
21
import android .content .Context ;
22
+ import android .os .Looper ;
22
23
import android .util .Log ;
23
24
24
25
import org .apache .http .Header ;
@@ -339,8 +340,8 @@ public void setThreadPool(ThreadPoolExecutor threadPool) {
339
340
}
340
341
341
342
/**
342
- * Returns the current executor service used. By default,
343
- * Executors.newFixedThreadPool() is used.
343
+ * Returns the current executor service used. By default, Executors.newFixedThreadPool() is
344
+ * used.
344
345
*
345
346
* @return current executor service used
346
347
*/
@@ -353,7 +354,7 @@ public ExecutorService getThreadPool() {
353
354
*
354
355
* @return The default threading pool to be used
355
356
*/
356
- protected ExecutorService getDefaultThreadPool () {
357
+ protected ExecutorService getDefaultThreadPool () {
357
358
return Executors .newCachedThreadPool ();
358
359
}
359
360
@@ -578,13 +579,27 @@ public void clearBasicAuth() {
578
579
* @param mayInterruptIfRunning specifies if active requests should be cancelled along with
579
580
* pending requests.
580
581
*/
581
- public void cancelRequests (Context context , boolean mayInterruptIfRunning ) {
582
- List <RequestHandle > requestList = requestMap .get (context );
583
- if (requestList != null ) {
584
- for (RequestHandle requestHandle : requestList ) {
585
- requestHandle .cancel (mayInterruptIfRunning );
582
+ public void cancelRequests (final Context context , final boolean mayInterruptIfRunning ) {
583
+ if (context == null ) {
584
+ Log .e (LOG_TAG , "Passed null Context to cancelRequests" );
585
+ return ;
586
+ }
587
+ Runnable r = new Runnable () {
588
+ @ Override
589
+ public void run () {
590
+ List <RequestHandle > requestList = requestMap .get (context );
591
+ if (requestList != null ) {
592
+ for (RequestHandle requestHandle : requestList ) {
593
+ requestHandle .cancel (mayInterruptIfRunning );
594
+ }
595
+ requestMap .remove (context );
596
+ }
586
597
}
587
- requestMap .remove (context );
598
+ };
599
+ if (Looper .myLooper () == Looper .getMainLooper ()) {
600
+ new Thread (r ).start ();
601
+ } else {
602
+ r .run ();
588
603
}
589
604
}
590
605
0 commit comments