|
1 | 1 | package com.loopj.android.http.sample;
|
2 | 2 |
|
3 |
| -import android.app.Activity; |
| 3 | +import org.apache.http.Header; |
4 | 4 |
|
5 |
| -public class PostSample extends Activity { |
| 5 | +import com.loopj.android.http.AsyncHttpClient; |
| 6 | +import com.loopj.android.http.AsyncHttpResponseHandler; |
6 | 7 |
|
| 8 | +public class PostSample extends SampleParentActivity { |
| 9 | + private static final String LOG_TAG = "PostSample"; |
| 10 | + |
| 11 | + @Override |
| 12 | + protected void executeSample(AsyncHttpClient client, String URL, AsyncHttpResponseHandler responseHandler) { |
| 13 | + client.post(this, URL, null, responseHandler); |
| 14 | + } |
| 15 | + |
| 16 | + @Override |
| 17 | + protected int getSampleTitle() { |
| 18 | + return R.string.title_post_sample; |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + protected boolean isRequestBodyAllowed() { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + protected boolean isRequestHeadersAllowed() { |
| 28 | + return true; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + protected String getDefaultURL() { |
| 33 | + return "http://www.google.com"; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + protected AsyncHttpResponseHandler getResponseHandler() { |
| 38 | + return new AsyncHttpResponseHandler() { |
| 39 | + |
| 40 | + @Override |
| 41 | + public void onStart() { |
| 42 | + clearOutputs(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void onSuccess(int statusCode, Header[] headers, byte[] response) { |
| 47 | + debugHeaders(LOG_TAG, headers); |
| 48 | + debugStatusCode(LOG_TAG, statusCode); |
| 49 | + debugResponse(LOG_TAG, new String(response)); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) { |
| 54 | + debugHeaders(LOG_TAG, headers); |
| 55 | + debugStatusCode(LOG_TAG, statusCode); |
| 56 | + debugThrowable(LOG_TAG, e); |
| 57 | + if (errorResponse != null) { |
| 58 | + debugResponse(LOG_TAG, new String(errorResponse)); |
| 59 | + } |
| 60 | + } |
| 61 | + }; |
| 62 | + } |
7 | 63 | }
|
| 64 | + |
0 commit comments