Java tutorial
/* Android Asynchronous Http Client Copyright (c) 2011 James Smith <james@loopj.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.uwindsor.elgg.project.http; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.HttpResponseException; import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.util.EntityUtils; import android.os.Handler; import android.os.Looper; import android.os.Message; /** * Used to intercept and handle the responses from requests made using {@link AsyncHttpClient}. The * {@link #onSuccess(String)} method is designed to be anonymously overridden with your own response handling code. * <p> * Additionally, you can override the {@link #onFailure(Throwable)}, {@link #onStart()}, and {@link #onFinish()} methods * as required. * <p> * For example: * <p> * * <pre> * AsyncHttpClient client = new AsyncHttpClient(); * client.get("http://www.google.com", new AsyncHttpResponseHandler() { * * @Override * public void onStart() { * * // Initiated the request * } * * @Override * public void onSuccess(String response) { * * // Successfully got a response * } * * @Override * public void onFailure(Throwable e) { * * // Response failed :( * } * * @Override * public void onFinish() { * * // Completed the request (either success or failure) * } * }); * </pre> */ public class AsyncHttpResponseHandler { private static final int SUCCESS_MESSAGE = 0; private static final int FAILURE_MESSAGE = 1; private static final int START_MESSAGE = 2; private static final int FINISH_MESSAGE = 3; private Handler handler; /** * Creates a new AsyncHttpResponseHandler */ public AsyncHttpResponseHandler() { // Set up a handler to post events back to the correct thread if possible if (Looper.myLooper() != null) { handler = new Handler() { @Override public void handleMessage(Message msg) { AsyncHttpResponseHandler.this.handleMessage(msg); } }; } } // // Callbacks to be overridden, typically anonymously // /** * Fired when the request is started, override to handle in your own code */ public void onStart() { } /** * Fired in all cases when the request is finished, after both success and failure, override to handle in your own * code */ public void onFinish() { } /** * Fired when a request returns successfully, override to handle in your own code * * @param content * the body of the HTTP response from the server */ public void onSuccess(String content) { } /** * Fired when a request fails to complete, override to handle in your own code * * @param error * the underlying cause of the failure */ public void onFailure(Throwable error) { } // // Pre-processing of messages (executes in background threadpool thread) // protected void sendSuccessMessage(String responseBody) { sendMessage(obtainMessage(SUCCESS_MESSAGE, responseBody)); } protected void sendFailureMessage(Throwable e) { sendMessage(obtainMessage(FAILURE_MESSAGE, e)); } protected void sendStartMessage() { sendMessage(obtainMessage(START_MESSAGE, null)); } protected void sendFinishMessage() { sendMessage(obtainMessage(FINISH_MESSAGE, null)); } // // Pre-processing of messages (in original calling thread, typically the UI thread) // protected void handleSuccessMessage(String responseBody) { onSuccess(responseBody); } protected void handleFailureMessage(Throwable e) { onFailure(e); } // Methods which emulate android's Handler and Message methods protected void handleMessage(Message msg) { switch (msg.what) { case SUCCESS_MESSAGE: handleSuccessMessage((String) msg.obj); break; case FAILURE_MESSAGE: handleFailureMessage((Throwable) msg.obj); break; case START_MESSAGE: onStart(); break; case FINISH_MESSAGE: onFinish(); break; } } protected void sendMessage(Message msg) { if (handler != null) { handler.sendMessage(msg); } else { handleMessage(msg); } } protected Message obtainMessage(int responseMessage, Object response) { Message msg = null; if (handler != null) { msg = this.handler.obtainMessage(responseMessage, response); } else { msg = new Message(); msg.what = responseMessage; msg.obj = response; } return msg; } // Interface to AsyncHttpRequest void sendResponseMessage(HttpResponse response) { StatusLine status = response.getStatusLine(); if (status.getStatusCode() >= 300) { sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase())); } else { try { HttpEntity entity = null; HttpEntity temp = response.getEntity(); if (temp != null) { entity = new BufferedHttpEntity(temp); } sendSuccessMessage(EntityUtils.toString(entity)); } catch (IOException e) { sendFailureMessage(e); } } } }