|
| 1 | +package com.loopj.android.http.sample; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonFactory; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.loopj.android.http.AsyncHttpClient; |
| 6 | +import com.loopj.android.http.RequestFactory; |
| 7 | +import com.loopj.android.http.handlers.BaseJsonHttpResponseHandler; |
| 8 | +import com.loopj.android.http.interfaces.ResponseHandlerInterface; |
| 9 | +import com.loopj.android.http.sample.util.SampleJSON; |
| 10 | +import com.loopj.android.http.sample.util.SampleJSONWrapper; |
| 11 | +import com.loopj.android.http.utils.RequestHandle; |
| 12 | + |
| 13 | +import cz.msebera.android.httpclient.Header; |
| 14 | +import cz.msebera.android.httpclient.HttpEntity; |
| 15 | + |
| 16 | +public class JsonSample extends SampleParentActivity { |
| 17 | + @Override |
| 18 | + public ResponseHandlerInterface getResponseHandler() { |
| 19 | + return new BaseJsonHttpResponseHandler<SampleJSON>() { |
| 20 | + @Override |
| 21 | + public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) { |
| 22 | + debugHeaders(getLogTag(), headers); |
| 23 | + debugStatusCode(getLogTag(), statusCode); |
| 24 | + debugResponse(getLogTag(), rawJsonResponse); |
| 25 | + debugResponse(getLogTag(), formatResponse(response)); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) { |
| 30 | + debugHeaders(getLogTag(), headers); |
| 31 | + debugStatusCode(getLogTag(), statusCode); |
| 32 | + debugThrowable(getLogTag(), throwable); |
| 33 | + debugResponse(getLogTag(), rawJsonData); |
| 34 | + debugResponse(getLogTag(), formatResponse(errorResponse)); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable { |
| 39 | + return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSONWrapper.class).next().getHeaders(); |
| 40 | + } |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + protected String formatResponse(SampleJSON json) { |
| 45 | + StringBuilder buf = new StringBuilder(); |
| 46 | + |
| 47 | + buf.append("Parsed SampleJSON object is ").append(json == null ? "null" : "not-null"); |
| 48 | + if (json != null) { |
| 49 | + buf.append("\nAccepts: ").append(json.getAccept()); |
| 50 | + buf.append("\nAcceptLanguage: ").append(json.getAcceptLanguage()); |
| 51 | + buf.append("\nReferer: ").append(json.getReferer()); |
| 52 | + buf.append("\nUserAgent: ").append(json.getUserAgent()); |
| 53 | + buf.append("\nConnection: ").append(json.getConnection()); |
| 54 | + } |
| 55 | + return buf.toString(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getDefaultURL() { |
| 60 | + return PROTOCOL + "httpbin.org/headers"; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean isRequestHeadersAllowed() { |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean isRequestBodyAllowed() { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public int getSampleTitle() { |
| 75 | + return R.string.title_json_sample; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) { |
| 80 | + return client.sendRequest(RequestFactory.get(URL, headers), responseHandler); |
| 81 | + } |
| 82 | +} |
0 commit comments