mobi.cangol.mobile.http.AsyncHttpResponseHandler Maven / Gradle / Ivy
/*
Android Asynchronous Http Client
Copyright (c) 2011 James Smith
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 mobi.cangol.mobile.http;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.io.IOException;
import java.lang.ref.WeakReference;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class AsyncHttpResponseHandler {
protected static final int SUCCESS_MESSAGE = 0;
protected static final int FAILURE_MESSAGE = 1;
protected static final int START_MESSAGE = 2;
protected static final int FINISH_MESSAGE = 3;
private Handler handler;
public AsyncHttpResponseHandler() {
// Set up a handler to post events back to the correct thread if possible
if (Looper.myLooper() != null) {
handler = new Handler() {
public void handleMessage(Message msg) {
AsyncHttpResponseHandler.this.handleMessage(msg);
}
};
}
}
public AsyncHttpResponseHandler(Context context) {
// Set up a handler to post events back to the correct thread if possible
if (Looper.myLooper() != null) {
handler = new InternalHandler(context);
}
}
public void onStart() {
}
public void onFinish() {
}
public void onSuccess(String content) {
}
public void onSuccess(int statusCode, String content) {
onSuccess(content);
}
public void onFailure(Throwable error) {
}
public void onFailure(Throwable error, String content) {
// By default, call the deprecated onFailure(Throwable) for compatibility
onFailure(error);
}
protected void sendSuccessMessage(int statusCode, String responseBody) {
sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{new Integer(statusCode), responseBody}));
}
protected void sendFailureMessage(Throwable e, String responseBody) {
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{e, responseBody}));
}
protected void sendFailureMessage(Throwable e, byte[] responseBody) {
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{e, responseBody}));
}
protected void sendStartMessage() {
sendMessage(obtainMessage(START_MESSAGE, null));
}
protected void sendFinishMessage() {
sendMessage(obtainMessage(FINISH_MESSAGE, null));
}
protected void handleSuccessMessage(int statusCode, String responseBody) {
onSuccess(statusCode, responseBody);
}
protected void handleFailureMessage(Throwable e, String responseBody) {
onFailure(e, responseBody);
}
// 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(), (String) response[1]);
break;
case FAILURE_MESSAGE:
response = (Object[]) msg.obj;
handleFailureMessage((Throwable) response[0], (String) response[1]);
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;
}
void sendResponseMessage(Response response) {
ResponseBody requestBody = response.body();
if (response.isSuccessful()) {
try {
sendSuccessMessage(response.code(), requestBody.string());
} catch (IOException e) {
sendFailureMessage(e, response.message());
}
} else {
sendFailureMessage(new IOException("code=" + response.code()), response.message());
}
response.close();
}
final static class InternalHandler extends Handler {
private final WeakReference mContext;
public InternalHandler(Context context) {
mContext = new WeakReference(context);
}
public void handleMessage(Message msg) {
Context context = mContext.get();
if (context != null) {
handleMessage(msg);
}
}
}
}