package cn.ptaxi.yuekeduocx.driver.netUtls;
|
|
import android.graphics.Bitmap;
|
import android.util.Log;
|
|
import com.google.gson.JsonSyntaxException;
|
|
import java.lang.ref.WeakReference;
|
import java.net.ConnectException;
|
import java.net.SocketTimeoutException;
|
|
import cn.ptaxi.yuekeduocx.driver.base.MyApplication;
|
import cn.ptaxi.yuekeduocx.driver.utils.LogUtils;
|
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 MyUploadObserver implements Observer<ResponseBody>, Disposable {
|
|
WeakReference<BaseActivity> activities;
|
WeakReference<BaseFragment> fragments;
|
|
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 MyUploadObserver(BaseActivity activity) {
|
activities = new WeakReference<>(activity);
|
if (activity != null) {
|
activity.compositeSubscription.add(this);
|
}
|
}
|
|
public MyUploadObserver(BaseFragment fragment) {
|
fragments = new WeakReference<BaseFragment>(fragment);
|
if (fragment != null && !fragment.isDetached()) {
|
fragment.addDisposable(this);
|
}
|
}
|
|
public MyUploadObserver() {
|
}
|
|
@Override
|
public void onSubscribe(@NonNull Disposable d) {
|
|
}
|
|
@Override
|
public void onNext(@NonNull ResponseBody responseBody) {
|
dismissDialog();
|
try {
|
String responseString = responseBody.string().replaceAll("\"","");
|
LogUtils.e("responseString", "**" + responseString);
|
if (StringUtils.isEmpty(responseString)) { //判空
|
int code = -1;
|
String msg = PARSER_ERROR;
|
onError(code, msg, responseString);
|
return;
|
}
|
if (responseString.startsWith("http")) {
|
success(responseString);
|
} else {
|
onError(-1, "上传失败", 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(MyApplication.Companion.getInstance()).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() {
|
|
}
|
}
|