From af7e9e4bcd90504d6a665dbb21635eb1733fe025 Mon Sep 17 00:00:00 2001 From: Marek Sebera Date: Fri, 17 Jul 2015 00:18:15 +0200 Subject: [PATCH] Modified #754 solution to place TAG within ResponseHandlerInterface implementation, so it can be used as identification of request/response pair, Closing #552 --- .../loopj/android/http/AsyncHttpRequest.java | 20 +++++++++++++++++++ .../http/AsyncHttpResponseHandler.java | 14 ++++++++++++- .../com/loopj/android/http/RequestHandle.java | 14 +++++++------ .../http/ResponseHandlerInterface.java | 15 ++++++++++++++ 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/library/src/main/java/com/loopj/android/http/AsyncHttpRequest.java b/library/src/main/java/com/loopj/android/http/AsyncHttpRequest.java index 748d1570f..5d7790548 100755 --- a/library/src/main/java/com/loopj/android/http/AsyncHttpRequest.java +++ b/library/src/main/java/com/loopj/android/http/AsyncHttpRequest.java @@ -234,4 +234,24 @@ public boolean cancel(boolean mayInterruptIfRunning) { request.abort(); return isCancelled(); } + + /** + * Will set Object as TAG to this request, wrapped by WeakReference + * + * @param TAG Object used as TAG to this RequestHandle + * @return this AsyncHttpRequest to allow fluid syntax + */ + public AsyncHttpRequest setRequestTag(Object TAG) { + this.responseHandler.setTag(TAG); + return this; + } + + /** + * Will return TAG of this AsyncHttpRequest + * + * @return Object TAG, can be null, if it's been already garbage collected + */ + public Object getTag() { + return this.responseHandler.getTag(); + } } diff --git a/library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java b/library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java index 2ee33a60b..2cc417b9e 100755 --- a/library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java +++ b/library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.io.InputStream; +import java.lang.ref.WeakReference; import java.net.URI; /** @@ -102,6 +103,7 @@ public abstract class AsyncHttpResponseHandler implements ResponseHandlerInterfa private URI requestURI = null; private Header[] requestHeaders = null; private Looper looper = null; + private WeakReference TAG = new WeakReference(null); /** * Creates a new AsyncHttpResponseHandler @@ -147,6 +149,16 @@ public AsyncHttpResponseHandler(boolean usePoolThread) { } } + @Override + public void setTag(Object TAG) { + this.TAG = new WeakReference(TAG); + } + + @Override + public Object getTag() { + return this.TAG.get(); + } + @Override public URI getRequestURI() { return this.requestURI; @@ -399,7 +411,7 @@ protected void handleMessage(Message message) { onCancel(); break; } - } catch(Throwable error) { + } catch (Throwable error) { onUserException(error); } } diff --git a/library/src/main/java/com/loopj/android/http/RequestHandle.java b/library/src/main/java/com/loopj/android/http/RequestHandle.java index a21a7a22b..1eb3c0d34 100755 --- a/library/src/main/java/com/loopj/android/http/RequestHandle.java +++ b/library/src/main/java/com/loopj/android/http/RequestHandle.java @@ -27,7 +27,6 @@ */ public class RequestHandle { private final WeakReference request; - private WeakReference TAG = new WeakReference(null); public RequestHandle(AsyncHttpRequest request) { this.request = new WeakReference(request); @@ -98,22 +97,25 @@ public boolean shouldBeGarbageCollected() { } /** - * Will set Object as TAG to this request, wrapped by WeakReference + * Will set Object as TAG to underlying AsyncHttpRequest * - * @param tag Object used as TAG to this RequestHandle + * @param tag Object used as TAG to underlying AsyncHttpRequest * @return this RequestHandle to allow fluid syntax */ public RequestHandle setTag(Object tag) { - TAG = new WeakReference(tag); + AsyncHttpRequest _request = request.get(); + if (_request != null) + _request.setRequestTag(tag); return this; } /** - * Will return TAG of this RequestHandle + * Will return TAG of underlying AsyncHttpRequest if it's not already GCed * * @return Object TAG, can be null */ public Object getTag() { - return TAG.get(); + AsyncHttpRequest _request = request.get(); + return _request == null ? null : _request.getTag(); } } \ No newline at end of file diff --git a/library/src/main/java/com/loopj/android/http/ResponseHandlerInterface.java b/library/src/main/java/com/loopj/android/http/ResponseHandlerInterface.java index e438e68a2..fcba44961 100755 --- a/library/src/main/java/com/loopj/android/http/ResponseHandlerInterface.java +++ b/library/src/main/java/com/loopj/android/http/ResponseHandlerInterface.java @@ -171,4 +171,19 @@ public interface ResponseHandlerInterface { * @param response The response to post-process */ void onPostProcessResponse(ResponseHandlerInterface instance, HttpResponse response); + + /** + * Will set TAG to ResponseHandlerInterface implementation, which can be then obtained + * in implemented methods, such as onSuccess, onFailure, ... + * + * @param TAG Object to be set as TAG, will be placed in WeakReference + */ + void setTag(Object TAG); + + /** + * Will retrieve TAG Object if it's not already freed from memory + * + * @return Object TAG or null if it's been garbage collected + */ + Object getTag(); }