Skip to content

Commit dc97281

Browse files
author
Noor Dawod
committed
Sample for pre- and post-processing requests and responses.
1 parent 4717ae8 commit dc97281

File tree

5 files changed

+175
-3
lines changed

5 files changed

+175
-3
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
Android Asynchronous Http Client Sample
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;
20+
21+
import android.content.Context;
22+
import android.graphics.Color;
23+
import android.util.Log;
24+
import com.loopj.android.http.AsyncHttpClient;
25+
import com.loopj.android.http.AsyncHttpRequest;
26+
import com.loopj.android.http.AsyncHttpResponseHandler;
27+
import com.loopj.android.http.RequestHandle;
28+
import com.loopj.android.http.ResponseHandlerInterface;
29+
import java.util.Locale;
30+
31+
import org.apache.http.Header;
32+
import org.apache.http.HttpEntity;
33+
import org.apache.http.HttpResponse;
34+
import org.apache.http.client.methods.HttpUriRequest;
35+
import org.apache.http.impl.client.AbstractHttpClient;
36+
import org.apache.http.impl.client.DefaultHttpClient;
37+
import org.apache.http.protocol.HttpContext;
38+
39+
public class PrePostProcessingSample extends SampleParentActivity {
40+
41+
private static final String LOG_TAG = "PrePostProcessingSample";
42+
43+
protected static final int LIGHTGREY = Color.parseColor("#E0E0E0");
44+
protected static final int DARKGREY = Color.parseColor("#888888");
45+
46+
@Override
47+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
48+
return client.post(this, URL, headers, entity, null, responseHandler);
49+
}
50+
51+
@Override
52+
public int getSampleTitle() {
53+
return R.string.title_pre_post_processing;
54+
}
55+
56+
@Override
57+
public boolean isRequestBodyAllowed() {
58+
return true;
59+
}
60+
61+
@Override
62+
public boolean isRequestHeadersAllowed() {
63+
return true;
64+
}
65+
66+
@Override
67+
public String getDefaultURL() {
68+
return "http://httpbin.org/post";
69+
}
70+
71+
@Override
72+
public AsyncHttpRequest getHttpRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
73+
return new PrePostProcessRequest(client, httpContext, uriRequest, responseHandler);
74+
}
75+
76+
@Override
77+
public ResponseHandlerInterface getResponseHandler() {
78+
return new AsyncHttpResponseHandler() {
79+
80+
@Override
81+
public void onPreProcessResponse(ResponseHandlerInterface instance, HttpResponse response) {
82+
debugProcessing(LOG_TAG, "Pre",
83+
"Response is about to be pre-processed", LIGHTGREY);
84+
}
85+
86+
@Override
87+
public void onPostProcessResponse(ResponseHandlerInterface instance, HttpResponse response) {
88+
debugProcessing(LOG_TAG, "Post",
89+
"Response is about to be post-processed", DARKGREY);
90+
}
91+
92+
@Override
93+
public void onStart() {
94+
clearOutputs();
95+
}
96+
97+
@Override
98+
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
99+
debugHeaders(LOG_TAG, headers);
100+
debugStatusCode(LOG_TAG, statusCode);
101+
debugResponse(LOG_TAG, new String(response));
102+
}
103+
104+
@Override
105+
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
106+
debugHeaders(LOG_TAG, headers);
107+
debugStatusCode(LOG_TAG, statusCode);
108+
debugThrowable(LOG_TAG, e);
109+
if (errorResponse != null) {
110+
debugResponse(LOG_TAG, new String(errorResponse));
111+
}
112+
}
113+
};
114+
}
115+
116+
protected void debugProcessing(String TAG, String state, String message, final int color) {
117+
final String debugMessage = String.format(Locale.US, "%s-processing: %s", state, message);
118+
Log.d(TAG, debugMessage);
119+
runOnUiThread(new Runnable() {
120+
@Override
121+
public void run() {
122+
addView(getColoredView(color, debugMessage));
123+
}
124+
});
125+
}
126+
127+
private class PrePostProcessRequest extends AsyncHttpRequest {
128+
129+
public PrePostProcessRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, ResponseHandlerInterface responseHandler) {
130+
super(client, context, request, responseHandler);
131+
}
132+
133+
@Override
134+
public void onPreProcessRequest(AsyncHttpRequest request) {
135+
debugProcessing(LOG_TAG, "Pre",
136+
"Request is about to be pre-processed", LIGHTGREY);
137+
}
138+
139+
@Override
140+
public void onPostProcessRequest(AsyncHttpRequest request) {
141+
debugProcessing(LOG_TAG, "Post",
142+
"Request is about to be post-processed", DARKGREY);
143+
}
144+
}
145+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@
1818

1919
package com.loopj.android.http.sample;
2020

21+
import android.content.Context;
2122
import com.loopj.android.http.AsyncHttpClient;
23+
import com.loopj.android.http.AsyncHttpRequest;
2224
import com.loopj.android.http.RequestHandle;
2325
import com.loopj.android.http.ResponseHandlerInterface;
2426

2527
import org.apache.http.Header;
2628
import org.apache.http.HttpEntity;
2729

2830
import java.util.List;
31+
import org.apache.http.client.methods.HttpUriRequest;
32+
import org.apache.http.impl.client.DefaultHttpClient;
33+
import org.apache.http.protocol.HttpContext;
2934

3035
public interface SampleInterface {
3136

@@ -45,6 +50,8 @@ public interface SampleInterface {
4550

4651
void setAsyncHttpClient(AsyncHttpClient client);
4752

53+
AsyncHttpRequest getHttpRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context);
54+
4855
ResponseHandlerInterface getResponseHandler();
4956

5057
String getDefaultURL();

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package com.loopj.android.http.sample;
2020

2121
import android.app.Activity;
22+
import android.content.Context;
2223
import android.graphics.Color;
2324
import android.os.Bundle;
2425
import android.util.Log;
@@ -30,7 +31,9 @@
3031
import android.widget.TextView;
3132

3233
import com.loopj.android.http.AsyncHttpClient;
34+
import com.loopj.android.http.AsyncHttpRequest;
3335
import com.loopj.android.http.RequestHandle;
36+
import com.loopj.android.http.ResponseHandlerInterface;
3437

3538
import org.apache.http.Header;
3639
import org.apache.http.HttpEntity;
@@ -44,10 +47,22 @@
4447
import java.util.LinkedList;
4548
import java.util.List;
4649
import java.util.Locale;
50+
import org.apache.http.client.methods.HttpUriRequest;
51+
import org.apache.http.impl.client.DefaultHttpClient;
52+
import org.apache.http.protocol.HttpContext;
4753

4854
public abstract class SampleParentActivity extends Activity implements SampleInterface {
4955

50-
private AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
56+
private AsyncHttpClient asyncHttpClient = new AsyncHttpClient() {
57+
58+
@Override
59+
protected AsyncHttpRequest newAsyncHttpRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
60+
AsyncHttpRequest httpRequest = getHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context);
61+
return httpRequest == null
62+
? super.newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context)
63+
: httpRequest;
64+
}
65+
};
5166
private EditText urlEditText, headersEditText, bodyEditText;
5267
private LinearLayout responseLayout;
5368
private final List<RequestHandle> requestHandles = new LinkedList<RequestHandle>();
@@ -88,6 +103,11 @@ protected void onCreate(Bundle savedInstanceState) {
88103
}
89104
}
90105

106+
@Override
107+
public AsyncHttpRequest getHttpRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
108+
return null;
109+
}
110+
91111
public List<RequestHandle> getRequestHandles() {
92112
return requestHandles;
93113
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class WaypointsActivity extends ListActivity {
5353
new SampleConfig(R.string.title_retry_handler, RetryRequestSample.class),
5454
new SampleConfig(R.string.title_range_sample, RangeResponseSample.class),
5555
new SampleConfig(R.string.title_401_unauth, Http401AuthSample.class),
56-
new SampleConfig(R.string.title_async_background_thread, AsyncBackgroundThreadSample.class)
56+
new SampleConfig(R.string.title_pre_post_processing, PrePostProcessingSample.class)
5757
};
5858

5959
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
<string name="title_retry_handler">Retrying requests by Exception</string>
3636
<string name="title_range_sample">Range response handling</string>
3737
<string name="title_401_unauth">401 basic authentication</string>
38-
<string name="title_async_background_thread">Async on background thread</string>
38+
<string name="title_pre_post_processing">Pre-/Post-processing</string>
3939
</resources>

0 commit comments

Comments
 (0)