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.drjing.xibao.common.http; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.Log; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.squareup.okhttp.Headers; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import java.io.IOException; /** * Used to intercept and handle the responses from requests made using * The {@link #onSuccess(String)} method is * designed to be anonymously overridden with your own response handling code. * 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, String response) { * // Response failed :( * } * * @Override * public void onFinish() { * // Completed the request (either success or failure) * } * }); * </pre> */ public class AsyncHttpResponseHandler { protected static final int SUCCESS_MESSAGE = 0; protected static final int FAILURE_MESSAGE = 1; 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 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 returns successfully, override to handle in your own code * * @param statusCode the status code of the response * @param headers the headers of the HTTP response * @param content the body of the HTTP response from the server */ public void onSuccess(int statusCode, Headers headers, String content) { onSuccess(statusCode, content); } /** * Fired when a request returns successfully, override to handle in your own code * * @param statusCode the status code of the response * @param content the body of the HTTP response from the server */ public void onSuccess(int statusCode, String content) { try { JSONObject object = JSON.parseObject(content); onSuccess(content); } catch (Exception e) { e.printStackTrace(); Log.e("onSuccessTAG", content); onSuccess("{status:400}"); } } /** * Called when the request could not be executed due to cancellation, a * connectivity problem or timeout. Because networks can fail during an * exchange, it is possible that the remote server accepted the request * before the failure. */ public void onFailure(Request request, IOException e) { } // // ??Handler sendMessageUI // protected void sendSuccessMessage(Response response) { try { sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[] { new Integer(response.code()), response.headers(), response.body().string() })); } catch (IOException e) { e.printStackTrace(); } } protected void sendFailureMessage(Request request, IOException e) { sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[] { request, e })); } // // Pre-processing of messages (in original calling thread, typically the UI thread) // protected void handleSuccessMessage(int statusCode, Headers headers, String responseBody) { onSuccess(statusCode, headers, responseBody); } protected void handleFailureMessage(Request request, IOException e) { onFailure(request, e); } // Methods which emulate android's Handler and Message methods protected void handleMessage(Message msg) { Object[] response; switch (msg.what) { case SUCCESS_MESSAGE: response = (Object[]) msg.obj; handleSuccessMessage(((Integer) response[0]).intValue(), (Headers) response[1], (String) response[2]); break; case FAILURE_MESSAGE: response = (Object[]) msg.obj; Log.e("handleMessage", response[0].getClass().getName()); handleFailureMessage((Request) response[0], (IOException) response[1]); 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 = Message.obtain(); msg.what = responseMessage; msg.obj = response; } return msg; } }