Skip to content

Commit 3751ae6

Browse files
committed
Added IntentService Synchronized GET request sample
1 parent 5bc70ad commit 3751ae6

File tree

7 files changed

+254
-2
lines changed

7 files changed

+254
-2
lines changed

sample/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
<activity android:name=".CancelAllRequestsSample"/>
3030
<activity android:name=".CancelRequestHandleSample"/>
3131
<activity android:name=".SynchronousClientSample"/>
32+
<activity android:name=".IntentServiceSample"/>
3233
<activity android:name=".SaxSample"/>
34+
35+
<service android:name=".services.ExampleIntentService"/>
3336
</application>
3437

3538
</manifest>
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.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.content.IntentFilter;
7+
8+
import com.loopj.android.http.AsyncHttpClient;
9+
import com.loopj.android.http.RequestHandle;
10+
import com.loopj.android.http.ResponseHandlerInterface;
11+
import com.loopj.android.http.sample.services.ExampleIntentService;
12+
import com.loopj.android.http.sample.util.IntentUtil;
13+
14+
import org.apache.http.Header;
15+
import org.apache.http.HttpEntity;
16+
17+
public class IntentServiceSample extends SampleParentActivity {
18+
19+
public static final String LOG_TAG = "IntentServiceSample";
20+
public static final String ACTION_START = "SYNC_START";
21+
public static final String ACTION_RETRY = "SYNC_RETRY";
22+
public static final String ACTION_CANCEL = "SYNC_CANCEL";
23+
public static final String ACTION_SUCCESS = "SYNC_SUCCESS";
24+
public static final String ACTION_FAILURE = "SYNC_FAILURE";
25+
public static final String ACTION_FINISH = "SYNC_FINISH";
26+
public static final String[] ALLOWED_ACTIONS = {ACTION_START,
27+
ACTION_RETRY, ACTION_CANCEL, ACTION_SUCCESS, ACTION_FAILURE, ACTION_FINISH};
28+
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
29+
@Override
30+
public void onReceive(Context context, Intent intent) {
31+
switch (intent.getAction()) {
32+
case ACTION_START:
33+
clearOutputs();
34+
addView(getColoredView(LIGHTBLUE, "Request started"));
35+
break;
36+
case ACTION_FINISH:
37+
addView(getColoredView(LIGHTBLUE, "Request finished"));
38+
break;
39+
case ACTION_CANCEL:
40+
addView(getColoredView(LIGHTBLUE, "Request cancelled"));
41+
break;
42+
case ACTION_RETRY:
43+
addView(getColoredView(LIGHTBLUE, "Request retried"));
44+
break;
45+
case ACTION_FAILURE:
46+
debugThrowable(LOG_TAG, (Throwable) intent.getSerializableExtra(ExampleIntentService.INTENT_THROWABLE));
47+
case ACTION_SUCCESS:
48+
debugStatusCode(LOG_TAG, intent.getIntExtra(ExampleIntentService.INTENT_STATUS_CODE, 0));
49+
debugHeaders(LOG_TAG, IntentUtil.deserializeHeaders(intent.getStringArrayExtra(ExampleIntentService.INTENT_HEADERS)));
50+
byte[] returnedBytes = intent.getByteArrayExtra(ExampleIntentService.INTENT_DATA);
51+
if (returnedBytes != null) {
52+
debugResponse(LOG_TAG, new String(returnedBytes));
53+
}
54+
break;
55+
}
56+
}
57+
};
58+
59+
@Override
60+
protected void onStart() {
61+
super.onStart();
62+
IntentFilter iFilter = new IntentFilter();
63+
for (String action : ALLOWED_ACTIONS) {
64+
iFilter.addAction(action);
65+
}
66+
registerReceiver(broadcastReceiver, iFilter);
67+
}
68+
69+
@Override
70+
protected void onPause() {
71+
super.onPause();
72+
unregisterReceiver(broadcastReceiver);
73+
}
74+
75+
@Override
76+
public ResponseHandlerInterface getResponseHandler() {
77+
// no response handler on activity
78+
return null;
79+
}
80+
81+
@Override
82+
public String getDefaultURL() {
83+
return "https://httpbin.org/get";
84+
}
85+
86+
@Override
87+
public boolean isRequestHeadersAllowed() {
88+
return false;
89+
}
90+
91+
@Override
92+
public boolean isRequestBodyAllowed() {
93+
return false;
94+
}
95+
96+
@Override
97+
public int getSampleTitle() {
98+
return R.string.title_intent_service_sample;
99+
}
100+
101+
@Override
102+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
103+
Intent serviceCall = new Intent(this, ExampleIntentService.class);
104+
serviceCall.putExtra(ExampleIntentService.INTENT_URL, URL);
105+
startService(serviceCall);
106+
return null;
107+
}
108+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public class WaypointsActivity extends ListActivity {
4444
R.string.title_threading_timeout,
4545
R.string.title_cancel_all,
4646
R.string.title_cancel_handle,
47-
R.string.title_synchronous
47+
R.string.title_synchronous,
48+
R.string.title_intent_service_sample
4849
};
4950
private static final Class[] targets = {
5051
GetSample.class,
@@ -60,7 +61,8 @@ public class WaypointsActivity extends ListActivity {
6061
ThreadingTimeoutSample.class,
6162
CancelAllRequestsSample.class,
6263
CancelRequestHandleSample.class,
63-
SynchronousClientSample.class
64+
SynchronousClientSample.class,
65+
IntentServiceSample.class
6466
};
6567

6668
@Override
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.loopj.android.http.sample.services;
2+
3+
import android.app.IntentService;
4+
import android.content.Intent;
5+
import android.util.Log;
6+
7+
import com.loopj.android.http.AsyncHttpClient;
8+
import com.loopj.android.http.AsyncHttpResponseHandler;
9+
import com.loopj.android.http.SyncHttpClient;
10+
import com.loopj.android.http.sample.IntentServiceSample;
11+
import com.loopj.android.http.sample.util.IntentUtil;
12+
13+
import org.apache.http.Header;
14+
15+
public class ExampleIntentService extends IntentService {
16+
17+
public static final String LOG_TAG = "ExampleIntentService:IntentServiceSample";
18+
public static final String INTENT_URL = "INTENT_URL";
19+
public static final String INTENT_STATUS_CODE = "INTENT_STATUS_CODE";
20+
public static final String INTENT_HEADERS = "INTENT_HEADERS";
21+
public static final String INTENT_DATA = "INTENT_DATA";
22+
public static final String INTENT_THROWABLE = "INTENT_THROWABLE";
23+
24+
private AsyncHttpClient aClient = new SyncHttpClient();
25+
26+
public ExampleIntentService() {
27+
super("ExampleIntentService");
28+
}
29+
30+
@Override
31+
public void onStart(Intent intent, int startId) {
32+
Log.d(LOG_TAG, "onStart()");
33+
super.onStart(intent, startId);
34+
}
35+
36+
@Override
37+
protected void onHandleIntent(Intent intent) {
38+
if (intent != null && intent.hasExtra(INTENT_URL)) {
39+
aClient.get(this, intent.getStringExtra(INTENT_URL), new AsyncHttpResponseHandler() {
40+
@Override
41+
public void onStart() {
42+
sendBroadcast(new Intent(IntentServiceSample.ACTION_START));
43+
Log.d(LOG_TAG, "onStart");
44+
}
45+
46+
@Override
47+
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
48+
Intent broadcast = new Intent(IntentServiceSample.ACTION_SUCCESS);
49+
broadcast.putExtra(INTENT_STATUS_CODE, statusCode);
50+
broadcast.putExtra(INTENT_HEADERS, IntentUtil.serializeHeaders(headers));
51+
broadcast.putExtra(INTENT_DATA, responseBody);
52+
sendBroadcast(broadcast);
53+
Log.d(LOG_TAG, "onSuccess");
54+
}
55+
56+
@Override
57+
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
58+
Intent broadcast = new Intent(IntentServiceSample.ACTION_FAILURE);
59+
broadcast.putExtra(INTENT_STATUS_CODE, statusCode);
60+
broadcast.putExtra(INTENT_HEADERS, IntentUtil.serializeHeaders(headers));
61+
broadcast.putExtra(INTENT_DATA, responseBody);
62+
broadcast.putExtra(INTENT_THROWABLE, error);
63+
sendBroadcast(broadcast);
64+
Log.d(LOG_TAG, "onFailure");
65+
}
66+
67+
@Override
68+
public void onCancel() {
69+
sendBroadcast(new Intent(IntentServiceSample.ACTION_CANCEL));
70+
Log.d(LOG_TAG, "onCancel");
71+
}
72+
73+
@Override
74+
public void onRetry(int retryNo) {
75+
sendBroadcast(new Intent(IntentServiceSample.ACTION_RETRY));
76+
Log.d(LOG_TAG, String.format("onRetry: %d", retryNo));
77+
}
78+
79+
@Override
80+
public void onFinish() {
81+
sendBroadcast(new Intent(IntentServiceSample.ACTION_FINISH));
82+
Log.d(LOG_TAG, "onFinish");
83+
}
84+
});
85+
}
86+
}
87+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Android Asynchronous Http Client
3+
Copyright (c) 2014 Marek Sebera <[email protected]>
4+
http://loopj.com
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package com.loopj.android.http.sample.services;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.loopj.android.http.sample.util;
2+
3+
import org.apache.http.Header;
4+
import org.apache.http.message.BasicHeader;
5+
6+
public class IntentUtil {
7+
8+
public static String[] serializeHeaders(Header[] headers) {
9+
if (headers == null) {
10+
return new String[0];
11+
}
12+
String[] rtn = new String[headers.length * 2];
13+
int index = -1;
14+
for (Header h : headers) {
15+
rtn[++index] = h.getName();
16+
rtn[++index] = h.getValue();
17+
}
18+
return rtn;
19+
}
20+
21+
public static Header[] deserializeHeaders(String[] serialized) {
22+
if (serialized == null || serialized.length % 2 != 0) {
23+
return new Header[0];
24+
}
25+
Header[] headers = new Header[serialized.length / 2];
26+
for (int i = 0, h = 0; h < headers.length; i++, h++) {
27+
headers[h] = new BasicHeader(serialized[i], serialized[++i]);
28+
}
29+
return headers;
30+
}
31+
32+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<string name="title_synchronous">Synchronous GET request</string>
1616
<string name="title_threading_timeout">Threading timeouts</string>
1717
<string name="title_gzip_sample">GET Gzipped JSON and parse it</string>
18+
<string name="title_intent_service_sample">IntentService Synchronised Request</string>
1819
<string name="button_run">Run</string>
1920
<string name="label_headers">Headers (key=val, one per line)</string>
2021
<string name="label_req_body">Request body</string>

0 commit comments

Comments
 (0)