Skip to content

Commit af7e9e4

Browse files
committed
Modified android-async-http#754 solution to place TAG within ResponseHandlerInterface implementation, so it can be used as identification of request/response pair, Closing android-async-http#552
1 parent 0107712 commit af7e9e4

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,24 @@ public boolean cancel(boolean mayInterruptIfRunning) {
234234
request.abort();
235235
return isCancelled();
236236
}
237+
238+
/**
239+
* Will set Object as TAG to this request, wrapped by WeakReference
240+
*
241+
* @param TAG Object used as TAG to this RequestHandle
242+
* @return this AsyncHttpRequest to allow fluid syntax
243+
*/
244+
public AsyncHttpRequest setRequestTag(Object TAG) {
245+
this.responseHandler.setTag(TAG);
246+
return this;
247+
}
248+
249+
/**
250+
* Will return TAG of this AsyncHttpRequest
251+
*
252+
* @return Object TAG, can be null, if it's been already garbage collected
253+
*/
254+
public Object getTag() {
255+
return this.responseHandler.getTag();
256+
}
237257
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import java.io.IOException;
3333
import java.io.InputStream;
34+
import java.lang.ref.WeakReference;
3435
import java.net.URI;
3536

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

106108
/**
107109
* Creates a new AsyncHttpResponseHandler
@@ -147,6 +149,16 @@ public AsyncHttpResponseHandler(boolean usePoolThread) {
147149
}
148150
}
149151

152+
@Override
153+
public void setTag(Object TAG) {
154+
this.TAG = new WeakReference<Object>(TAG);
155+
}
156+
157+
@Override
158+
public Object getTag() {
159+
return this.TAG.get();
160+
}
161+
150162
@Override
151163
public URI getRequestURI() {
152164
return this.requestURI;
@@ -399,7 +411,7 @@ protected void handleMessage(Message message) {
399411
onCancel();
400412
break;
401413
}
402-
} catch(Throwable error) {
414+
} catch (Throwable error) {
403415
onUserException(error);
404416
}
405417
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
*/
2828
public class RequestHandle {
2929
private final WeakReference<AsyncHttpRequest> request;
30-
private WeakReference<Object> TAG = new WeakReference<Object>(null);
3130

3231
public RequestHandle(AsyncHttpRequest request) {
3332
this.request = new WeakReference<AsyncHttpRequest>(request);
@@ -98,22 +97,25 @@ public boolean shouldBeGarbageCollected() {
9897
}
9998

10099
/**
101-
* Will set Object as TAG to this request, wrapped by WeakReference
100+
* Will set Object as TAG to underlying AsyncHttpRequest
102101
*
103-
* @param tag Object used as TAG to this RequestHandle
102+
* @param tag Object used as TAG to underlying AsyncHttpRequest
104103
* @return this RequestHandle to allow fluid syntax
105104
*/
106105
public RequestHandle setTag(Object tag) {
107-
TAG = new WeakReference<Object>(tag);
106+
AsyncHttpRequest _request = request.get();
107+
if (_request != null)
108+
_request.setRequestTag(tag);
108109
return this;
109110
}
110111

111112
/**
112-
* Will return TAG of this RequestHandle
113+
* Will return TAG of underlying AsyncHttpRequest if it's not already GCed
113114
*
114115
* @return Object TAG, can be null
115116
*/
116117
public Object getTag() {
117-
return TAG.get();
118+
AsyncHttpRequest _request = request.get();
119+
return _request == null ? null : _request.getTag();
118120
}
119121
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,19 @@ public interface ResponseHandlerInterface {
171171
* @param response The response to post-process
172172
*/
173173
void onPostProcessResponse(ResponseHandlerInterface instance, HttpResponse response);
174+
175+
/**
176+
* Will set TAG to ResponseHandlerInterface implementation, which can be then obtained
177+
* in implemented methods, such as onSuccess, onFailure, ...
178+
*
179+
* @param TAG Object to be set as TAG, will be placed in WeakReference
180+
*/
181+
void setTag(Object TAG);
182+
183+
/**
184+
* Will retrieve TAG Object if it's not already freed from memory
185+
*
186+
* @return Object TAG or null if it's been garbage collected
187+
*/
188+
Object getTag();
174189
}

0 commit comments

Comments
 (0)