|
| 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 | +} |
0 commit comments