package com.runt.open.mvvm.retrofit.observable; import android.accounts.NetworkErrorException; import android.util.Log; import androidx.annotation.NonNull; import androidx.lifecycle.MutableLiveData; import com.runt.open.mvvm.data.BaseApiResult; import java.io.IOException; import java.lang.reflect.ParameterizedType; import java.net.SocketTimeoutException; import io.reactivex.SingleObserver; import io.reactivex.observers.DisposableObserver; import retrofit2.Response; /** * 网络请求观察 * Created by Administrator on 2021/11/11 0011. */ public abstract class HttpObserver extends DisposableObserver> implements SingleObserver> { final String TAG = "HttpObserver"; MutableLiveData resultLive; public HttpObserver(MutableLiveData resultLive) { this.resultLive = resultLive; } @Override public void onNext(Response response) { onExcuted(response); } @Override public void onError(Throwable throwable) { Log.i("subscribe","onError"); try { Log.e(TAG,this.getClass().getSimpleName()+" mes:"+throwable.getMessage()); Class entityClass = (Class) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; M t = entityClass.newInstance();//实例化一个泛型 t.code = 410; if( throwable instanceof SocketTimeoutException){ t.msg = "服务请求超时,请稍候再试";//设置错误信息 }else if( throwable instanceof NetworkErrorException){ t.msg = "网络连接不畅,请检查您的网络设置";//设置错误信息 }else{ t.msg = throwable.getMessage();//设置错误信息 } resultLive.setValue(t); } catch (ClassCastException e) { e.printStackTrace(); M t = (M) new BaseApiResult(); t.code = 409; t.msg = "实例化对象未指定泛型实体类"; resultLive.setValue(t); } catch (Exception e) { e.printStackTrace(); M t = (M) new BaseApiResult(); t.code = 409; t.msg = e.getMessage(); resultLive.setValue(t); } } @Override public void onSuccess(Response response) { onExcuted(response); } private void onExcuted(@NonNull Response response){ if(response.body() != null){ resultLive.setValue(response.body()); }else{ try { String error = response.errorBody().string(); Log.i("subscribe","onExcuted "+error); onError(new Throwable(error)); } catch (IOException e) { e.printStackTrace(); } } } @Override public void onComplete() { Log.i("subscribe","onComplete"); } }