package com.fuban.driver.netUtls;
|
|
import android.content.Context;
|
import android.graphics.Bitmap;
|
import android.util.Log;
|
|
import com.fuban.driver.bean.FaceBean;
|
import com.fuban.driver.utils.LogUtils;
|
import com.google.gson.Gson;
|
import com.google.gson.JsonSyntaxException;
|
|
import java.lang.ref.WeakReference;
|
import java.net.ConnectException;
|
import java.net.SocketTimeoutException;
|
|
import cn.sinata.xldutils.activitys.BaseActivity;
|
import cn.sinata.xldutils.fragment.BaseFragment;
|
import cn.sinata.xldutils.net.utils.ResultException;
|
import cn.sinata.xldutils.utils.StringUtils;
|
import cn.sinata.xldutils.utils.Toast;
|
import cn.sinata.xldutils.utils.Utils;
|
import cn.sinata.xldutils.xldUtils;
|
import io.reactivex.Observer;
|
import io.reactivex.annotations.NonNull;
|
import io.reactivex.disposables.Disposable;
|
import okhttp3.ResponseBody;
|
import retrofit2.HttpException;
|
|
/**
|
* 自定义Observer类处理网络数据
|
* Created by Administrator on 2018/1/25.
|
*/
|
|
public abstract class FaceObserver implements Observer<ResponseBody>, Disposable {
|
|
WeakReference<BaseActivity> activities;
|
WeakReference<BaseFragment> fragments;
|
Context context;
|
|
public static final String REQUEST_ERROR = "请求出错!";
|
public static final String PARSER_ERROR = "数据解析出错!";
|
public static final String SERVER_ERROR = "服务器异常,请稍后重试!";
|
public static final String NET_ERROR = "未连接到服务器!";
|
public static final String NET_TIMEOUT = "链接超时,请稍后再试!";
|
|
|
public FaceObserver(BaseActivity activity) {
|
activities = new WeakReference<>(activity);
|
if (activity != null) {
|
this.context = activity;
|
activity.compositeSubscription.add(this);
|
}
|
}
|
|
public FaceObserver(BaseFragment fragment) {
|
fragments = new WeakReference<BaseFragment>(fragment);
|
if (fragment != null && !fragment.isDetached()) {
|
this.context = fragment.getContext();
|
fragment.addDisposable(this);
|
}
|
}
|
|
public FaceObserver(Context context) {
|
this.context = context;
|
}
|
|
@Override
|
public void onSubscribe(@NonNull Disposable d) {
|
|
}
|
|
@Override
|
public void onNext(@NonNull ResponseBody responseBody) {
|
dismissDialog();
|
try {
|
String responseString = responseBody.string();
|
LogUtils.e("responseString", "**" + responseString);
|
if (StringUtils.isEmpty(responseString)) { //判空
|
int code = -1;
|
String msg = PARSER_ERROR;
|
onError(code, msg,responseString);
|
return;
|
}
|
// success(responseString);
|
FaceBean baseBean = new Gson().fromJson(responseString, FaceBean.class);
|
if (baseBean.getStatus() == Api.SUCCESS) {
|
success(responseString);
|
} else {
|
// onError(baseBean.getStatus(), baseBean.getMsg(),responseString);
|
// success(responseString);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public void onError(@NonNull Throwable e) {
|
dismissDialog();
|
if (xldUtils.DEBUG) {
|
e.printStackTrace();
|
}
|
Utils.systemErr(e);
|
Log.e("responseString", "responseString get " + e.toString());
|
int code = -1;
|
String msg = REQUEST_ERROR;
|
if (e instanceof JsonSyntaxException ||
|
e instanceof NumberFormatException ||
|
e instanceof IllegalStateException) {
|
msg = PARSER_ERROR;
|
} else if (e instanceof HttpException) {
|
msg = SERVER_ERROR;
|
} else if (e instanceof ConnectException) {
|
msg = NET_ERROR;
|
} else if (e instanceof SocketTimeoutException) {
|
msg = NET_TIMEOUT;
|
} else if (e instanceof ResultException) {
|
code = ((ResultException) e).getCode();
|
msg = e.getMessage();
|
}
|
try {
|
onError(code, msg,"");
|
} catch (Exception es) {
|
es.printStackTrace();
|
}
|
|
}
|
|
protected void onError(int code, String msg,String resposeString) {
|
if (shouldShowErrorToast()) {
|
showToast(msg);
|
}
|
}
|
|
protected boolean shouldShowErrorToast() {
|
return true;
|
}
|
|
@Override
|
public void onComplete() {
|
|
}
|
|
/**
|
* 显示toast
|
*
|
* @param s 提示文字
|
*/
|
protected void showToast(String s) {
|
Toast.create(context).show(s);
|
}
|
|
/**
|
* 访问成功回调抽象方法
|
*
|
* @param responseString 返回的数据
|
*/
|
public abstract void success(String responseString);
|
|
/**
|
* 访问成功回调方法
|
*
|
* @param bitmap 获取的Bitmap
|
*/
|
public void success(Bitmap bitmap) {
|
}
|
|
private void dismissDialog() {
|
if (activities != null) {
|
BaseActivity activity = activities.get();
|
if (activity != null) {
|
activity.dismissDialog();
|
|
}
|
}
|
if (fragments != null) {
|
BaseFragment fragment = fragments.get();
|
if (fragment != null) {
|
fragment.dismissDialog();
|
|
}
|
}
|
}
|
|
@Override
|
public boolean isDisposed() {
|
return false;
|
}
|
|
@Override
|
public void dispose() {
|
|
}
|
}
|