public interface ScenesService {
@POST("{url}")
Observable<RBaseResponse> executePost(
@Path("url") String url,
@Body Map<String, String> maps);
@FormUrlEncoded
@POST("{url}")
Observable<RBaseResponse<List<ScenesListBean>>> getScenesList(
@Path("url") String url,
@FieldMap Map<String, String> maps);
@FormUrlEncoded
@POST("{url}")
Observable<RBaseResponse<List<DeviceRoughBean>>> getdevicelist(
@Path("url") String url,
@FieldMap Map<String, String> maps);
}
public class RBaseResponse<T> {
String msg="";
String note="";
int code;
T data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "RBaseResponse{" +
"msg='" + msg + '\'' +
", note='" + note + '\'' +
", code=" + code +
", data=" + data +
'}';
}
}
public abstract class BaseObserver< T extends RBaseResponse > implements Observer<T> { /** * token错误 */ public static final int TOKEN_ERROR = 403; private static final int UNAUTHORIZED = 401; private static final int FORBIDDEN = 403; private static final int NOT_FOUND = 404; private static final int REQUEST_TIMEOUT = 408; private static final int INTERNAL_SERVER_ERROR = 500; private static final int SERVICE_UNAVAILABLE = 503; String tips=""; BasePopupView mxpopip; BaseViewModel mviewmodel; @Override public void onNext(T response) { if(mviewmodel!=null){ mviewmodel.dismissDialog(); } if(response.code==200){ //成功 onSuccess(response); }else if(response.code==TOKEN_ERROR){ //成功 ToastUtils.showShort("帐号信息已过期.请重新登录"); Rootfrag re=new Rootfrag(); re.setGoout(true); RxBus.getDefault().post(re); }else{ //失败 onFailing(response); } } public BaseObserver(BaseViewModel mviewmodel,String tips) { super(); this.tips=tips; this.mviewmodel=mviewmodel; } public BaseObserver(BaseViewModel mviewmodel) { super(); this.mviewmodel=mviewmodel; } public BaseObserver( ) { super(); } @Override public void onSubscribe(Disposable d) { if(mviewmodel!=null){ mviewmodel.setDisposableEvent(d); mviewmodel.showDialog(tips); } } @Override public void onComplete() { KLog.i("onComplete"); } @Override public void onError(Throwable e) { ResponseThrowable ex; if (e instanceof HttpException) { HttpException httpException = (HttpException) e; ex = new ResponseThrowable(e, ERROR.HTTP_ERROR); switch (httpException.code()) { case UNAUTHORIZED: ex.message = "操作未授权"; break; case FORBIDDEN: ex.message = "请求被拒绝"; break; case NOT_FOUND: ex.message = "资源不存在"; break; case REQUEST_TIMEOUT: ex.message = "服务器执行超时"; break; case INTERNAL_SERVER_ERROR: ex.message = "服务器内部错误"; break; case SERVICE_UNAVAILABLE: ex.message = "服务器不可用"; break; default: ex.message = "网络错误"; break; } } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException || e instanceof MalformedJsonException) { ex = new ResponseThrowable(e, ERROR.PARSE_ERROR); ex.message = "解析错误"; } else if (e instanceof ConnectException) { ex = new ResponseThrowable(e, ERROR.NETWORD_ERROR); ex.message = "连接失败"; } else if (e instanceof javax.net.ssl.SSLException) { ex = new ResponseThrowable(e, ERROR.SSL_ERROR); ex.message = "证书验证失败"; } else if (e instanceof ConnectTimeoutException) { ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR); ex.message = "连接超时"; } else if (e instanceof java.net.SocketTimeoutException) { ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR); ex.message = "连接超时"; } else if (e instanceof java.net.UnknownHostException) { ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR); ex.message = "主机地址未知"; } else { ex = new ResponseThrowable(e, ERROR.UNKNOWN); ex.message = "未知错误"; } ToastUtils.showShort(ex.message); KLog.i("onError"); if(mviewmodel!=null){ mviewmodel.dismissDialog(); } KLog.e(e); } /** * 约定异常 这个具体规则需要与服务端或者领导商讨定义 */ class ERROR { /** * 未知错误 */ public static final int UNKNOWN = 1000; /** * 解析错误 */ public static final int PARSE_ERROR = 1001; /** * 网络错误 */ public static final int NETWORD_ERROR = 1002; /** * 协议出错 */ public static final int HTTP_ERROR = 1003; /** * 证书出错 */ public static final int SSL_ERROR = 1005; /** * 连接超时 */ public static final int TIMEOUT_ERROR = 1006; } public abstract void onSuccess(T response); public void onFailing(T response) { String message = response.msg; if (TextUtils.isEmpty(message)) { ToastUtils.showShort("请求错误"); } else { ToastUtils.showShort(message); } } }
在
RetrofitClient.getInstance().create(ScenesService.class).getScenesList(HttpConfig.scenesOrAF, map)
.compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new BaseObserver<RBaseResponse<List<ScenesListBean>>>() {
@Override
public void onSuccess(RBaseResponse<List<ScenesListBean>> response) {
KLog.a("tangshang==" + response.getCode());
// final List<ScenesListBean> deviceRoughBeans = response.getData();
}
});
subscribe方法中做处理,返回结果直接通过response获取