Skip to content

Commit 2447cba

Browse files
committed
Merge pull request android-async-http#852 from liangxiaofei1100/master
Fix java.util.ConcurrentModificationException of AsyncHttpClient.
2 parents a4e4da7 + fddb256 commit 2447cba

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -721,22 +721,28 @@ public void cancelRequests(final Context context, final boolean mayInterruptIfRu
721721
Log.e(LOG_TAG, "Passed null Context to cancelRequests");
722722
return;
723723
}
724-
Runnable r = new Runnable() {
725-
@Override
726-
public void run() {
727-
List<RequestHandle> requestList = requestMap.get(context);
728-
if (requestList != null) {
729-
for (RequestHandle requestHandle : requestList) {
730-
requestHandle.cancel(mayInterruptIfRunning);
731-
}
732-
requestMap.remove(context);
733-
}
734-
}
735-
};
724+
725+
final List<RequestHandle> requestList = requestMap.get(context);
726+
requestMap.remove(context);
727+
736728
if (Looper.myLooper() == Looper.getMainLooper()) {
737-
new Thread(r).start();
729+
Runnable runnable = new Runnable() {
730+
@Override
731+
public void run() {
732+
cancelRequests(requestList, mayInterruptIfRunning);
733+
}
734+
};
735+
threadPool.submit(runnable);
738736
} else {
739-
r.run();
737+
cancelRequests(requestList, mayInterruptIfRunning);
738+
}
739+
}
740+
741+
private void cancelRequests(final List<RequestHandle> requestList, final boolean mayInterruptIfRunning) {
742+
if (requestList != null) {
743+
for (RequestHandle requestHandle : requestList) {
744+
requestHandle.cancel(mayInterruptIfRunning);
745+
}
740746
}
741747
}
742748

0 commit comments

Comments
 (0)