From 5264ecfb4f763fa3d4d905b28747c2ae5951674c Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 23 Sep 2014 21:16:39 -0700 Subject: Fix cancelling network request from within main thread --- .../daneren2005/dsub/service/RESTMusicService.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/github') diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index 75923883..f4571ac1 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -64,6 +64,7 @@ import android.content.SharedPreferences; import android.graphics.Bitmap; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import android.os.Looper; import android.util.Log; import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.*; @@ -1521,7 +1522,7 @@ public class RESTMusicService implements MusicService { return executeWithRetry(context, rewrittenUrl, url, requestParams, parameterNames, parameterValues, headers, progressListener, task); } - private HttpResponse executeWithRetry(Context context, String url, String originalUrl, HttpParams requestParams, + private HttpResponse executeWithRetry(final Context context, String url, String originalUrl, HttpParams requestParams, List parameterNames, List parameterValues, List
headers, ProgressListener progressListener, SilentBackgroundTask task) throws Exception { // Strip out sensitive information from log @@ -1549,9 +1550,19 @@ public class RESTMusicService implements MusicService { public void onCancel() { try { isCancelled.set(true); - request.abort(); + if(Thread.currentThread() == Looper.getMainLooper().getThread()) { + new SilentBackgroundTask(context) { + @Override + protected Void doInBackground() throws Throwable { + request.abort(); + return null; + } + }.execute(); + } else { + request.abort(); + } } catch(Exception e) { - Log.e(TAG, "Failed to stop http task"); + Log.e(TAG, "Failed to stop http task", e); } } }); -- cgit v1.2.3