diff options
author | Scott Jackson <daneren2005@gmail.com> | 2014-09-23 21:16:39 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2014-09-23 21:16:39 -0700 |
commit | 5264ecfb4f763fa3d4d905b28747c2ae5951674c (patch) | |
tree | 801f88b18e472068453a9625eb21edab7b5dcf82 | |
parent | d6350dcc2cb5f33692318ca91b0133fde675a651 (diff) | |
download | dsub-5264ecfb4f763fa3d4d905b28747c2ae5951674c.tar.gz dsub-5264ecfb4f763fa3d4d905b28747c2ae5951674c.tar.bz2 dsub-5264ecfb4f763fa3d4d905b28747c2ae5951674c.zip |
Fix cancelling network request from within main thread
-rw-r--r-- | src/github/daneren2005/dsub/service/RESTMusicService.java | 17 |
1 files changed, 14 insertions, 3 deletions
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<String> parameterNames, List<Object> parameterValues, List<Header> 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<Void>(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); } } }); |