Skip to content

Commit 3a42acd

Browse files
committed
Added Synchronous request sample
1 parent 8800a7d commit 3a42acd

File tree

5 files changed

+137
-6
lines changed

5 files changed

+137
-6
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public interface SampleInterface {
1313

1414
List<RequestHandle> getRequestHandles();
1515

16+
void addRequestHandle(RequestHandle handle);
17+
1618
void onRunButtonPressed();
1719

1820
void onCancelButtonPressed();
@@ -23,6 +25,8 @@ public interface SampleInterface {
2325

2426
AsyncHttpClient getAsyncHttpClient();
2527

28+
void setAsyncHttpClient(AsyncHttpClient client);
29+
2630
ResponseHandlerInterface getResponseHandler();
2731

2832
String getDefaultURL();

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,29 @@ protected void onCreate(Bundle savedInstanceState) {
6060
headersLayout.setVisibility(isRequestHeadersAllowed() ? View.VISIBLE : View.GONE);
6161

6262
runButton.setOnClickListener(onClickListener);
63-
if (isCancelButtonAllowed() && cancelButton != null) {
64-
cancelButton.setVisibility(View.VISIBLE);
65-
cancelButton.setOnClickListener(onClickListener);
63+
if (cancelButton != null) {
64+
if (isCancelButtonAllowed()) {
65+
cancelButton.setVisibility(View.VISIBLE);
66+
cancelButton.setOnClickListener(onClickListener);
67+
} else {
68+
cancelButton.setEnabled(false);
69+
}
6670
}
6771
}
6872

6973
public List<RequestHandle> getRequestHandles() {
7074
return requestHandles;
7175
}
7276

77+
@Override
78+
public void addRequestHandle(RequestHandle handle) {
79+
if (null != handle) {
80+
requestHandles.add(handle);
81+
}
82+
}
83+
7384
public void onRunButtonPressed() {
74-
requestHandles.add(executeSample(getAsyncHttpClient(),
85+
addRequestHandle(executeSample(getAsyncHttpClient(),
7586
(urlEditText == null || urlEditText.getText() == null) ? getDefaultURL() : urlEditText.getText().toString(),
7687
getRequestHeaders(),
7788
getRequestEntity(),
@@ -201,4 +212,9 @@ public boolean isCancelButtonAllowed() {
201212
public AsyncHttpClient getAsyncHttpClient() {
202213
return this.asyncHttpClient;
203214
}
215+
216+
@Override
217+
public void setAsyncHttpClient(AsyncHttpClient client) {
218+
this.asyncHttpClient = client;
219+
}
204220
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.loopj.android.http.sample;
2+
3+
import android.os.Bundle;
4+
import android.util.Log;
5+
6+
import com.loopj.android.http.AsyncHttpClient;
7+
import com.loopj.android.http.AsyncHttpResponseHandler;
8+
import com.loopj.android.http.RequestHandle;
9+
import com.loopj.android.http.ResponseHandlerInterface;
10+
import com.loopj.android.http.SyncHttpClient;
11+
12+
import org.apache.http.Header;
13+
import org.apache.http.HttpEntity;
14+
15+
public class SynchronousClientSample extends GetSample {
16+
private static final String LOG_TAG = "SyncSample";
17+
18+
@Override
19+
protected void onCreate(Bundle savedInstanceState) {
20+
super.onCreate(savedInstanceState);
21+
setAsyncHttpClient(new SyncHttpClient());
22+
}
23+
24+
@Override
25+
public int getSampleTitle() {
26+
return R.string.title_synchronous;
27+
}
28+
29+
@Override
30+
public boolean isRequestBodyAllowed() {
31+
return false;
32+
}
33+
34+
@Override
35+
public boolean isRequestHeadersAllowed() {
36+
return true;
37+
}
38+
39+
@Override
40+
public String getDefaultURL() {
41+
return "https://httpbin.org/delay/6";
42+
}
43+
44+
@Override
45+
public RequestHandle executeSample(final AsyncHttpClient client, final String URL, final Header[] headers, HttpEntity entity, final ResponseHandlerInterface responseHandler) {
46+
if (client instanceof SyncHttpClient) {
47+
new Thread(new Runnable() {
48+
@Override
49+
public void run() {
50+
Log.d(LOG_TAG, "Before Request");
51+
client.get(SynchronousClientSample.this, URL, headers, null, responseHandler);
52+
Log.d(LOG_TAG, "After Request");
53+
}
54+
}).start();
55+
} else {
56+
Log.e(LOG_TAG, "Error, not using SyncHttpClient");
57+
}
58+
/**
59+
* SyncHttpClient does not return RequestHandle,
60+
* it executes each request directly,
61+
* therefore those requests are not in cancelable threads
62+
* */
63+
return null;
64+
}
65+
66+
@Override
67+
public ResponseHandlerInterface getResponseHandler() {
68+
return new AsyncHttpResponseHandler() {
69+
70+
@Override
71+
public void onStart() {
72+
runOnUiThread(new Runnable() {
73+
@Override
74+
public void run() {
75+
clearOutputs();
76+
}
77+
});
78+
}
79+
80+
@Override
81+
public void onSuccess(final int statusCode, final Header[] headers, final byte[] response) {
82+
runOnUiThread(new Runnable() {
83+
@Override
84+
public void run() {
85+
debugHeaders(LOG_TAG, headers);
86+
debugStatusCode(LOG_TAG, statusCode);
87+
debugResponse(LOG_TAG, new String(response));
88+
}
89+
});
90+
}
91+
92+
@Override
93+
public void onFailure(final int statusCode, final Header[] headers, final byte[] errorResponse, final Throwable e) {
94+
runOnUiThread(new Runnable() {
95+
@Override
96+
public void run() {
97+
debugHeaders(LOG_TAG, headers);
98+
debugStatusCode(LOG_TAG, statusCode);
99+
debugThrowable(LOG_TAG, e);
100+
if (errorResponse != null) {
101+
debugResponse(LOG_TAG, new String(errorResponse));
102+
}
103+
}
104+
});
105+
}
106+
};
107+
}
108+
}

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

Lines changed: 4 additions & 1 deletion
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", "CANCEL ALL REQUESTS", "CANCEL REQUEST HANDLE"};
12+
private static final String[] samples = new String[]{"GET", "POST", "DELETE", "PUT", "JSON", "FILE", "BINARY", "THREADING TIMEOUTS", "CANCEL ALL REQUESTS", "CANCEL REQUEST HANDLE", "SYNCHRONOUS CLIENT"};
1313

1414
@Override
1515
protected void onCreate(Bundle savedInstanceState) {
@@ -52,6 +52,9 @@ protected void onListItemClick(ListView l, View v, int position, long id) {
5252
case 9:
5353
targetClass = CancelRequestHandleSample.class;
5454
break;
55+
case 10:
56+
targetClass = SynchronousClientSample.class;
57+
break;
5558
}
5659
if (targetClass != null)
5760
startActivity(new Intent(this, targetClass));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<string name="title_binary_sample">GET binary data</string>
1212
<string name="title_cancel_all">Cancel all request</string>
1313
<string name="title_cancel_handle">Cancel request handle</string>
14-
<string name="title_synchronous">Synchronous requests</string>
14+
<string name="title_synchronous">Synchronous GET request</string>
1515
<string name="title_threading_timeout">Threading timeouts</string>
1616
<string name="button_run">Run</string>
1717
<string name="label_headers">Headers (key=val, one per line)</string>

0 commit comments

Comments
 (0)