nilupeng
2022-01-30 fa67d4417ec6949183038f99dff517f5e3c7cda6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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<M extends BaseApiResult> extends DisposableObserver<Response<M>> implements SingleObserver<Response<M>> {
 
    final String TAG = "HttpObserver";
 
    MutableLiveData<M> resultLive;
 
    public HttpObserver(MutableLiveData<M> resultLive) {
        this.resultLive = resultLive;
    }
 
 
    @Override
    public void onNext(Response<M> response) {
        onExcuted(response);
    }
 
    @Override
    public void onError(Throwable throwable) {
        Log.i("subscribe","onError");
 
        try {
            Log.e(TAG,this.getClass().getSimpleName()+" mes:"+throwable.getMessage());
            Class<M> entityClass = (Class<M>) ((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<String>();
            t.code = 409;
            t.msg = "实例化对象未指定泛型实体类";
            resultLive.setValue(t);
        } catch (Exception e) {
            e.printStackTrace();
            M t = (M) new BaseApiResult<String>();
            t.code = 409;
            t.msg = e.getMessage();
            resultLive.setValue(t);
        }
    }
 
    @Override
    public void onSuccess(Response<M> response) {
        onExcuted(response);
    }
 
    private void onExcuted(@NonNull Response<M> 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");
    }
 
}