Runt
2022-03-29 9aeecdbe43d4c4601710b6c7b301f41f55c07746
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
package com.auto.lyric.retrofit.observable;
 
import android.util.Log;
 
import androidx.lifecycle.MutableLiveData;
 
import com.auto.lyric.data.BaseApiResult;
 
import java.lang.reflect.ParameterizedType;
import java.net.SocketTimeoutException;
 
import io.reactivex.observers.DisposableObserver;
 
/**
 * 网络请求观察
 * Created by Administrator on 2021/11/11 0011.
 */
public abstract class HttpObserver<T extends BaseApiResult> extends DisposableObserver<T>{
 
    final String TAG = "HttpObserver";
 
    MutableLiveData<T> resultLive;
 
    public HttpObserver(MutableLiveData<T> resultLive) {
        this.resultLive = resultLive;
    }
 
 
    @Override
    public void onNext(T value) {
        resultLive.setValue(value);
    }
 
    @Override
    public void onError(Throwable throwable) {
        Log.i("subscribe","onError");
 
        try {
            Log.e(TAG,this.getClass().getSimpleName()+" "+throwable.getMessage());
            Class<T> entityClass = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            T t = entityClass.newInstance();//实例化一个泛型
            t.code = 410;
            if( throwable instanceof SocketTimeoutException){
                t.msg = "服务请求超时,请稍候再试";//设置错误信息
            }else{
                t.msg = "网络连接不畅,请检查您的网络设置";//设置错误信息
            }
            resultLive.setValue(t);
        } catch (ClassCastException e) {
            e.printStackTrace();
            T t = (T) new BaseApiResult<String>();
            t.code = 409;
            t.msg = "实例化对象未指定泛型实体类";
            resultLive.setValue(t);
        } catch (Exception e) {
            e.printStackTrace();
            T t = (T) new BaseApiResult<String>();
            t.code = 409;
            t.msg = e.getMessage();
            resultLive.setValue(t);
        }
    }
 
    @Override
    public void onComplete() {
        Log.i("subscribe","onComplete");
    }
}