Skip to content

Commit

Permalink
Modified #754 solution to place TAG within ResponseHandlerInterface i…
Browse files Browse the repository at this point in the history
…mplementation, so it can be used as identification of request/response pair, Closing #552
  • Loading branch information
smarek committed Jul 16, 2015
1 parent 0107712 commit af7e9e4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
20 changes: 20 additions & 0 deletions library/src/main/java/com/loopj/android/http/AsyncHttpRequest.java
Expand Up @@ -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();
}
}
Expand Up @@ -31,6 +31,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URI;

/**
Expand Down Expand Up @@ -102,6 +103,7 @@ public abstract class AsyncHttpResponseHandler implements ResponseHandlerInterfa
private URI requestURI = null;
private Header[] requestHeaders = null;
private Looper looper = null;
private WeakReference<Object> TAG = new WeakReference<Object>(null);

/**
* Creates a new AsyncHttpResponseHandler
Expand Down Expand Up @@ -147,6 +149,16 @@ public AsyncHttpResponseHandler(boolean usePoolThread) {
}
}

@Override
public void setTag(Object TAG) {
this.TAG = new WeakReference<Object>(TAG);
}

@Override
public Object getTag() {
return this.TAG.get();
}

@Override
public URI getRequestURI() {
return this.requestURI;
Expand Down Expand Up @@ -399,7 +411,7 @@ protected void handleMessage(Message message) {
onCancel();
break;
}
} catch(Throwable error) {
} catch (Throwable error) {
onUserException(error);
}
}
Expand Down
14 changes: 8 additions & 6 deletions library/src/main/java/com/loopj/android/http/RequestHandle.java
Expand Up @@ -27,7 +27,6 @@
*/
public class RequestHandle {
private final WeakReference<AsyncHttpRequest> request;
private WeakReference<Object> TAG = new WeakReference<Object>(null);

public RequestHandle(AsyncHttpRequest request) {
this.request = new WeakReference<AsyncHttpRequest>(request);
Expand Down Expand Up @@ -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<Object>(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();
}
}
Expand Up @@ -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();
}

0 comments on commit af7e9e4

Please sign in to comment.