Skip to content

Commit cf95c42

Browse files
committed
Updated Sample application to show capability of obtaining TAG within ResponseHandler, android-async-http#552
1 parent af7e9e4 commit cf95c42

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

sample/src/main/java/com/loopj/android/http/sample/CancelRequestByTagSample.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.util.Log;
2222

2323
import com.loopj.android.http.AsyncHttpClient;
24+
import com.loopj.android.http.AsyncHttpResponseHandler;
2425
import com.loopj.android.http.RequestHandle;
2526
import com.loopj.android.http.ResponseHandlerInterface;
2627

@@ -43,6 +44,39 @@ public void onCancelButtonPressed() {
4344
getAsyncHttpClient().cancelRequestsByTAG(REQUEST_TAG, false);
4445
}
4546

47+
@Override
48+
public ResponseHandlerInterface getResponseHandler() {
49+
return new AsyncHttpResponseHandler() {
50+
51+
private final int id = counter++;
52+
53+
@Override
54+
public void onStart() {
55+
setStatus(id, "TAG:" + getTag() + ", START");
56+
}
57+
58+
@Override
59+
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
60+
setStatus(id, "SUCCESS");
61+
}
62+
63+
@Override
64+
public void onFinish() {
65+
setStatus(id, "FINISH");
66+
}
67+
68+
@Override
69+
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
70+
setStatus(id, "FAILURE");
71+
}
72+
73+
@Override
74+
public void onCancel() {
75+
setStatus(id, "CANCEL");
76+
}
77+
};
78+
}
79+
4680
@Override
4781
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
4882
return client.get(this, URL, headers, null, responseHandler).setTag(REQUEST_TAG);

sample/src/main/java/com/loopj/android/http/sample/ThreadingTimeoutSample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
public class ThreadingTimeoutSample extends SampleParentActivity {
3232

3333
private static final String LOG_TAG = "ThreadingTimeoutSample";
34-
private final SparseArray<String> states = new SparseArray<String>();
35-
private int counter = 0;
34+
protected final SparseArray<String> states = new SparseArray<String>();
35+
protected int counter = 0;
3636

3737
@Override
3838
public int getSampleTitle() {
@@ -59,7 +59,7 @@ public String getDefaultURL() {
5959
return PROTOCOL + "httpbin.org/delay/6";
6060
}
6161

62-
private synchronized void setStatus(int id, String status) {
62+
protected synchronized void setStatus(int id, String status) {
6363
String current = states.get(id, null);
6464
states.put(id, current == null ? status : current + "," + status);
6565
clearOutputs();

0 commit comments

Comments
 (0)