Java tutorial
/* Android Asynchronous Http Client Sample Copyright (c) 2014 Marek Sebera <marek.sebera@gmail.com> http://loopj.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.loopj.android.http.sample; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.databind.ObjectMapper; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.BaseJsonHttpResponseHandler; import com.loopj.android.http.RequestHandle; import com.loopj.android.http.ResponseHandlerInterface; import com.loopj.android.http.sample.util.SampleJSON; import org.apache.http.Header; import org.apache.http.HttpEntity; public class JsonSample extends SampleParentActivity { private static final String LOG_TAG = "JsonSample"; @Override public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) { return client.get(this, URL, headers, null, responseHandler); } @Override public int getSampleTitle() { return R.string.title_json_sample; } @Override public boolean isRequestBodyAllowed() { return false; } @Override public boolean isRequestHeadersAllowed() { return false; } @Override public String getDefaultURL() { return PROTOCOL + "httpbin.org/headers"; } @Override public ResponseHandlerInterface getResponseHandler() { return new BaseJsonHttpResponseHandler<SampleJSON>() { @Override public void onStart() { clearOutputs(); } @Override public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) { debugHeaders(LOG_TAG, headers); debugStatusCode(LOG_TAG, statusCode); if (response != null) { debugResponse(LOG_TAG, rawJsonResponse); } } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) { debugHeaders(LOG_TAG, headers); debugStatusCode(LOG_TAG, statusCode); debugThrowable(LOG_TAG, throwable); if (errorResponse != null) { debugResponse(LOG_TAG, rawJsonData); } } @Override protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable { return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSON.class) .next(); } }; } }