Skip to content

Commit 9c836c6

Browse files
committed
Added cancel function to sample
1 parent 2bc4b72 commit 9c836c6

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ protected void onCreate(Bundle savedInstanceState) {
4747
headersEditText = (EditText) findViewById(R.id.edit_headers);
4848
bodyEditText = (EditText) findViewById(R.id.edit_body);
4949
Button runButton = (Button) findViewById(R.id.button_run);
50+
Button cancelButton = (Button) findViewById(R.id.button_cancel);
5051
LinearLayout headersLayout = (LinearLayout) findViewById(R.id.layout_headers);
5152
LinearLayout bodyLayout = (LinearLayout) findViewById(R.id.layout_body);
5253
responseLayout = (LinearLayout) findViewById(R.id.layout_response);
@@ -57,6 +58,10 @@ protected void onCreate(Bundle savedInstanceState) {
5758
headersLayout.setVisibility(isRequestHeadersAllowed() ? View.VISIBLE : View.GONE);
5859

5960
runButton.setOnClickListener(onClickListener);
61+
if (isCancelButtonAllowed() && cancelButton != null) {
62+
cancelButton.setVisibility(View.VISIBLE);
63+
cancelButton.setOnClickListener(onClickListener);
64+
}
6065
}
6166

6267
private View.OnClickListener onClickListener = new View.OnClickListener() {
@@ -70,6 +75,9 @@ public void onClick(View v) {
7075
getRequestEntity(),
7176
getResponseHandler());
7277
break;
78+
case R.id.button_cancel:
79+
asyncHttpClient.cancelRequests(SampleParentActivity.this, true);
80+
break;
7381
}
7482
}
7583
};
@@ -172,6 +180,10 @@ protected final void clearOutputs() {
172180
responseLayout.removeAllViews();
173181
}
174182

183+
protected boolean isCancelButtonAllowed() {
184+
return false;
185+
}
186+
175187
protected abstract int getSampleTitle();
176188

177189
protected abstract boolean isRequestBodyAllowed();

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
import org.apache.http.Header;
99
import org.apache.http.HttpEntity;
1010

11-
import java.util.Random;
12-
1311
public class ThreadingTimeoutSample extends SampleParentActivity {
1412

1513
private static final String LOG_TAG = "ThreadingTimeoutSample";
1614
private SparseArray<String> states = new SparseArray<String>();
15+
private int counter = 0;
1716

1817
@Override
1918
protected int getSampleTitle() {
@@ -30,6 +29,11 @@ protected boolean isRequestHeadersAllowed() {
3029
return false;
3130
}
3231

32+
@Override
33+
protected boolean isCancelButtonAllowed() {
34+
return true;
35+
}
36+
3337
@Override
3438
protected String getDefaultURL() {
3539
return "http://httpbin.org/delay/6";
@@ -48,7 +52,7 @@ private synchronized void setStatus(int id, String status) {
4852
protected AsyncHttpResponseHandler getResponseHandler() {
4953
return new AsyncHttpResponseHandler() {
5054

51-
private int id = new Random().nextInt(1000);
55+
private int id = counter++;
5256

5357
@Override
5458
public void onStart() {
@@ -69,6 +73,11 @@ public void onFinish() {
6973
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
7074
setStatus(id, "FAILURE");
7175
}
76+
77+
@Override
78+
public void onCancel() {
79+
setStatus(id, "CANCEL");
80+
}
7281
};
7382
}
7483

sample/src/main/res/layout/parent_layout.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
android:layout_width="wrap_content"
3232
android:layout_height="wrap_content"
3333
android:text="@string/button_run" />
34+
35+
<Button
36+
android:id="@+id/button_cancel"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:visibility="gone"
40+
android:text="@string/button_cancel" />
3441
</LinearLayout>
3542

3643
<LinearLayout

sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
<string name="button_run">Run</string>
1313
<string name="label_headers">Headers (key=val, one per line)</string>
1414
<string name="label_req_body">Request body</string>
15+
<string name="button_cancel">Cancel</string>
1516
</resources>

0 commit comments

Comments
 (0)