Skip to content

Commit 627ca53

Browse files
committed
Added more samples
1 parent 345d9c4 commit 627ca53

13 files changed

+93
-38
lines changed

sample/src/main/AndroidManifest.xml

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.loopj.android.http.sample"
4-
android:versionCode="1"
5-
android:versionName="1.0">
3+
package="com.loopj.android.http.sample"
4+
android:versionCode="1"
5+
android:versionName="1.0">
66

77
<uses-permission android:name="android.permission.INTERNET"/>
88

@@ -12,18 +12,20 @@
1212
android:theme="@style/AppTheme">
1313
<activity android:name=".WaypointsActivity">
1414
<intent-filter>
15-
<action android:name="android.intent.action.MAIN" />
16-
<category android:name="android.intent.category.LAUNCHER" />
15+
<action android:name="android.intent.action.MAIN"/>
16+
<category android:name="android.intent.category.LAUNCHER"/>
1717
</intent-filter>
1818
</activity>
19-
<activity android:name=".GetSample" />
20-
<activity android:name=".PostSample" />
21-
<activity android:name=".DeleteSample" />
22-
<activity android:name=".PutSample" />
23-
<activity android:name=".JsonSample" />
24-
<activity android:name=".FileSample" />
25-
<activity android:name=".BinarySample" />
26-
<activity android:name=".ThreadingTimeoutSample" />
19+
<activity android:name=".GetSample"/>
20+
<activity android:name=".PostSample"/>
21+
<activity android:name=".DeleteSample"/>
22+
<activity android:name=".PutSample"/>
23+
<activity android:name=".JsonSample"/>
24+
<activity android:name=".FileSample"/>
25+
<activity android:name=".BinarySample"/>
26+
<activity android:name=".ThreadingTimeoutSample"/>
27+
<activity android:name=".CancelAllRequests"/>
28+
<activity android:name=".CancelRequestHandle"/>
2729
</application>
2830

2931
</manifest>

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.loopj.android.http.AsyncHttpClient;
44
import com.loopj.android.http.AsyncHttpResponseHandler;
55
import com.loopj.android.http.BinaryHttpResponseHandler;
6+
import com.loopj.android.http.RequestHandle;
67

78
import org.apache.http.Header;
89
import org.apache.http.HttpEntity;
@@ -63,7 +64,7 @@ public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Th
6364
}
6465

6566
@Override
66-
protected void executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
67-
client.get(this, URL, headers, null, responseHandler);
67+
protected RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
68+
return client.get(this, URL, headers, null, responseHandler);
6869
}
6970
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.loopj.android.http.sample;
2+
3+
public class CancelAllRequests extends ThreadingTimeoutSample {
4+
5+
@Override
6+
protected void onCancelButtonPressed() {
7+
getAsyncHttpClient().cancelAllRequests(true);
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.loopj.android.http.sample;
2+
3+
import com.loopj.android.http.RequestHandle;
4+
5+
public class CancelRequestHandle extends ThreadingTimeoutSample {
6+
7+
@Override
8+
protected void onCancelButtonPressed() {
9+
for (RequestHandle handle : getRequestHandles()) {
10+
if (!handle.isCancelled() && !handle.isFinished()) {
11+
handle.cancel(true);
12+
}
13+
}
14+
}
15+
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.loopj.android.http.AsyncHttpClient;
44
import com.loopj.android.http.AsyncHttpResponseHandler;
5+
import com.loopj.android.http.RequestHandle;
56

67
import org.apache.http.Header;
78
import org.apache.http.HttpEntity;
@@ -10,8 +11,8 @@ public class DeleteSample extends SampleParentActivity {
1011
private static final String LOG_TAG = "DeleteSample";
1112

1213
@Override
13-
protected void executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
14-
client.delete(this, URL, headers, null, responseHandler);
14+
protected RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
15+
return client.delete(this, URL, headers, null, responseHandler);
1516
}
1617

1718
@Override

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.loopj.android.http.AsyncHttpClient;
66
import com.loopj.android.http.AsyncHttpResponseHandler;
77
import com.loopj.android.http.FileAsyncHttpResponseHandler;
8+
import com.loopj.android.http.RequestHandle;
89
import com.loopj.android.http.sample.util.FileUtil;
910

1011
import org.apache.http.Header;
@@ -76,7 +77,7 @@ private void debugFile(File file) {
7677
}
7778

7879
@Override
79-
protected void executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
80-
client.get(this, URL, headers, null, responseHandler);
80+
protected RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
81+
return client.get(this, URL, headers, null, responseHandler);
8182
}
8283
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.loopj.android.http.AsyncHttpClient;
44
import com.loopj.android.http.AsyncHttpResponseHandler;
5+
import com.loopj.android.http.RequestHandle;
56

67
import org.apache.http.Header;
78
import org.apache.http.HttpEntity;
@@ -10,8 +11,8 @@ public class GetSample extends SampleParentActivity {
1011
private static final String LOG_TAG = "GetSample";
1112

1213
@Override
13-
protected void executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
14-
client.get(this, URL, headers, null, responseHandler);
14+
protected RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
15+
return client.get(this, URL, headers, null, responseHandler);
1516
}
1617

1718
@Override

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.loopj.android.http.AsyncHttpClient;
66
import com.loopj.android.http.AsyncHttpResponseHandler;
77
import com.loopj.android.http.BaseJsonHttpResponseHandler;
8+
import com.loopj.android.http.RequestHandle;
89
import com.loopj.android.http.sample.util.SampleJSON;
910

1011
import org.apache.http.Header;
@@ -15,8 +16,8 @@ public class JsonSample extends SampleParentActivity {
1516
private static final String LOG_TAG = "JsonSample";
1617

1718
@Override
18-
protected void executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
19-
client.get(this, URL, headers, null, responseHandler);
19+
protected RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
20+
return client.get(this, URL, headers, null, responseHandler);
2021
}
2122

2223
@Override

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.loopj.android.http.AsyncHttpClient;
44
import com.loopj.android.http.AsyncHttpResponseHandler;
5+
import com.loopj.android.http.RequestHandle;
56

67
import org.apache.http.Header;
78
import org.apache.http.HttpEntity;
@@ -10,8 +11,8 @@ public class PostSample extends SampleParentActivity {
1011
private static final String LOG_TAG = "PostSample";
1112

1213
@Override
13-
protected void executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
14-
client.post(this, URL, headers, entity, null, responseHandler);
14+
protected RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
15+
return client.post(this, URL, headers, entity, null, responseHandler);
1516
}
1617

1718
@Override
@@ -51,7 +52,7 @@ public void onSuccess(int statusCode, Header[] headers, byte[] response) {
5152
}
5253

5354
@Override
54-
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
55+
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
5556
debugHeaders(LOG_TAG, headers);
5657
debugStatusCode(LOG_TAG, statusCode);
5758
debugThrowable(LOG_TAG, e);

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.loopj.android.http.AsyncHttpClient;
44
import com.loopj.android.http.AsyncHttpResponseHandler;
5+
import com.loopj.android.http.RequestHandle;
56

67
import org.apache.http.Header;
78
import org.apache.http.HttpEntity;
@@ -10,8 +11,8 @@ public class PutSample extends SampleParentActivity {
1011
private static final String LOG_TAG = "PutSample";
1112

1213
@Override
13-
protected void executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
14-
client.put(this, URL, headers, entity, null, responseHandler);
14+
protected RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
15+
return client.put(this, URL, headers, entity, null, responseHandler);
1516
}
1617

1718
@Override

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

+22-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import com.loopj.android.http.AsyncHttpClient;
1515
import com.loopj.android.http.AsyncHttpResponseHandler;
16+
import com.loopj.android.http.RequestHandle;
1617

1718
import org.apache.http.Header;
1819
import org.apache.http.HttpEntity;
@@ -23,6 +24,7 @@
2324
import java.io.StringWriter;
2425
import java.io.UnsupportedEncodingException;
2526
import java.util.ArrayList;
27+
import java.util.LinkedList;
2628
import java.util.List;
2729
import java.util.Locale;
2830

@@ -31,6 +33,7 @@ public abstract class SampleParentActivity extends Activity {
3133
private AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
3234
private EditText urlEditText, headersEditText, bodyEditText;
3335
private LinearLayout responseLayout;
36+
private final List<RequestHandle> requestHandles = new LinkedList<>();
3437

3538
private static final int LIGHTGREEN = Color.parseColor("#00FF66");
3639
private static final int LIGHTRED = Color.parseColor("#FF3300");
@@ -64,19 +67,31 @@ protected void onCreate(Bundle savedInstanceState) {
6467
}
6568
}
6669

70+
public List<RequestHandle> getRequestHandles() {
71+
return requestHandles;
72+
}
73+
74+
protected void onRunButtonPressed() {
75+
requestHandles.add(executeSample(getAsyncHttpClient(),
76+
(urlEditText == null || urlEditText.getText() == null) ? getDefaultURL() : urlEditText.getText().toString(),
77+
getRequestHeaders(),
78+
getRequestEntity(),
79+
getResponseHandler()));
80+
}
81+
82+
protected void onCancelButtonPressed() {
83+
asyncHttpClient.cancelRequests(SampleParentActivity.this, true);
84+
}
85+
6786
private View.OnClickListener onClickListener = new View.OnClickListener() {
6887
@Override
6988
public void onClick(View v) {
7089
switch (v.getId()) {
7190
case R.id.button_run:
72-
executeSample(getAsyncHttpClient(),
73-
(urlEditText == null || urlEditText.getText() == null) ? getDefaultURL() : urlEditText.getText().toString(),
74-
getRequestHeaders(),
75-
getRequestEntity(),
76-
getResponseHandler());
91+
onRunButtonPressed();
7792
break;
7893
case R.id.button_cancel:
79-
asyncHttpClient.cancelRequests(SampleParentActivity.this, true);
94+
onCancelButtonPressed();
8095
break;
8196
}
8297
}
@@ -198,5 +213,5 @@ protected AsyncHttpClient getAsyncHttpClient() {
198213
return this.asyncHttpClient;
199214
}
200215

201-
protected abstract void executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler);
216+
protected abstract RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler);
202217
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.loopj.android.http.AsyncHttpClient;
66
import com.loopj.android.http.AsyncHttpResponseHandler;
7+
import com.loopj.android.http.RequestHandle;
78

89
import org.apache.http.Header;
910
import org.apache.http.HttpEntity;
@@ -86,7 +87,7 @@ public int getCounter() {
8687
}
8788

8889
@Override
89-
protected void executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
90-
client.get(this, URL, headers, null, responseHandler);
90+
protected RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
91+
return client.get(this, URL, headers, null, responseHandler);
9192
}
9293
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class WaypointsActivity extends ListActivity {
1111

12-
private static final String[] samples = new String[]{"GET", "POST", "DELETE", "PUT", "JSON", "FILE", "BINARY", "THREADING TIMEOUTS"};
12+
private static final String[] samples = new String[]{"GET", "POST", "DELETE", "PUT", "JSON", "FILE", "BINARY", "THREADING TIMEOUTS", "CANCEL ALL REQUESTS", "CANCEL REQUEST HANDLE"};
1313

1414
@Override
1515
protected void onCreate(Bundle savedInstanceState) {
@@ -46,6 +46,12 @@ protected void onListItemClick(ListView l, View v, int position, long id) {
4646
case 7:
4747
targetClass = ThreadingTimeoutSample.class;
4848
break;
49+
case 8:
50+
targetClass = CancelAllRequests.class;
51+
break;
52+
case 9:
53+
targetClass = CancelRequestHandle.class;
54+
break;
4955
}
5056
if (targetClass != null)
5157
startActivity(new Intent(this, targetClass));

0 commit comments

Comments
 (0)