package cn.com.basic.face.discern.common;
|
|
import android.widget.Toast;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import cn.com.basic.face.base.BaseApplication;
|
|
import org.xutils.common.Callback;
|
import org.xutils.ex.HttpException;
|
|
import java.net.ConnectException;
|
import java.net.SocketTimeoutException;
|
import java.util.List;
|
|
public abstract class BaseCommonCallBack implements Callback.CommonCallback<ResultBean>{
|
|
public ResultBean resultBean;
|
|
public <T> T getBean(Class<T> requiredType) {
|
Object data = resultBean.getData();
|
if (data != null) {
|
if (data instanceof JSONObject) {
|
JSONObject jsonObj = (JSONObject) data;
|
return JSON.parseObject(jsonObj.toJSONString(), requiredType);
|
}
|
}
|
return null;
|
}
|
|
public <T> List<T> getList(Class<T> requiredType) {
|
Object data = resultBean.getData();
|
if (data != null) {
|
if (data instanceof JSONArray) {
|
JSONArray jsonArray = (JSONArray) data;
|
return JSON.parseArray(jsonArray.toJSONString(), requiredType);
|
}
|
}
|
return null;
|
}
|
|
public <T> List<T> getExtraList(Class<T> requiredType) {
|
Object data = resultBean.getExtraData();
|
if (data != null) {
|
if (data instanceof JSONArray) {
|
JSONArray jsonArray = (JSONArray) data;
|
return JSON.parseArray(jsonArray.toJSONString(), requiredType);
|
}
|
}
|
return null;
|
}
|
|
@Override
|
public void onSuccess(ResultBean resultBean) {
|
this.resultBean = resultBean;
|
if (this.resultBean.getCode().equals(ResultBean.APPLICATION_EXCEPTION)) {
|
Toast.makeText(BaseApplication.getInstance(), "操作失败:"+this.resultBean.getMessage(), Toast.LENGTH_SHORT ).show();
|
} else {
|
success();
|
}
|
}
|
|
public abstract void success();
|
|
@Override
|
public void onCancelled(CancelledException cex) {
|
System.out.print("hello");
|
}
|
|
@Override
|
public void onError(Throwable ex, boolean isOnCallback) {
|
if (ex instanceof SocketTimeoutException) {
|
Toast.makeText(BaseApplication.getInstance(), "socket连接超时", Toast.LENGTH_SHORT ).show();
|
return;
|
}
|
if (ex instanceof ConnectException) {
|
Toast.makeText(BaseApplication.getInstance(), "服务器连接失败", Toast.LENGTH_SHORT ).show();
|
return;
|
}
|
System.out.println(ex.getClass().getName());
|
if (ex instanceof org.xutils.ex.HttpException) {
|
org.xutils.ex.HttpException httpException = (org.xutils.ex.HttpException)ex;
|
Toast.makeText(BaseApplication.getInstance(), "服务器访问错误,错误代码:"+httpException.getErrorCode(), Toast.LENGTH_SHORT ).show();
|
return;
|
}
|
Toast.makeText(BaseApplication.getInstance(), "网络连接失败"+ex.getMessage(), Toast.LENGTH_SHORT ).show();
|
}
|
|
@Override
|
public void onFinished() {
|
System.out.print("hello");
|
}
|
|
public int getPageNum() {
|
return resultBean.getPageNum();
|
}
|
|
public int getTotalPages() {
|
return resultBean.getTotalPages();
|
}
|
|
public boolean hasMorePages() {
|
return resultBean.getPageNum() < resultBean.getTotalPages();
|
}
|
|
}
|