Runt
2024-09-28 1464250d6b481fb9274cc571ecbe031b71ab457c
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
package com.runt.open.mvi.retrofit.observable;
 
import android.util.Log;
 
import com.google.gson.Gson;
import com.runt.open.mvi.base.BaseActivity;
import com.runt.open.mvi.data.HttpApiResult;
 
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;
import java.util.regex.Pattern;
 
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.adapter.rxjava2.HttpException;
 
/**
 * 网络请求观察
 * Created by Administrator on 2021/11/11 0011.
 */
public abstract class HttpObserver<RESULT> implements Observer<HttpApiResult<RESULT>> {
 
    BaseActivity activity;
 
    public HttpObserver() {
    }
 
    public HttpObserver(BaseActivity activity) {
        this.activity = activity;
    }
 
    final String TAG = "HttpObserver";
 
    @Override
    public void onSubscribe(Disposable d) {
        Log.d(TAG,"onSubscribe "+hashCode());
    }
 
    @Override
    public void onNext(HttpApiResult<RESULT> httpResult) {
        Log.d(TAG,"onNext "+httpResult);
        if (httpResult != null && httpResult.code == 0) {
            onSuccess(httpResult.data);
        }else{
            onFailed(httpResult);//接口返回错误
        }
    }
 
    @Override
    public void onError(Throwable e) {
        Log.e(TAG,"onError "+e.getMessage()+" "+hashCode());
        int code = 600;
        String msg = "网络请求失败,请检查网络或稍后重试";
        if( e instanceof ConnectException || e instanceof TimeoutException
                || e instanceof SocketTimeoutException || e instanceof UnknownHostException){
            code = 601;
            msg = "网络请求失败,请检查网络或稍后重试";
        }else if( e instanceof HttpException){
            String regEx = "[^0-9]";
            Log.i(TAG,"code:"+ Pattern.compile(regEx).matcher(e.getMessage()).replaceAll(""));
            String error = Pattern.compile(regEx).matcher(e.getMessage()).replaceAll("");
            code = error.length()>0?Integer.parseInt(error):500;
            msg = error.length()>0?"服务器请求失败":"登录信息验证失败";
        }
        HttpApiResult httpResult = new Gson().fromJson("{'code':"+code+",'msg':'"+msg+"'}", HttpApiResult.class);
        onFailed(httpResult);
    }
 
    @Override
    public void onComplete() {
        Log.i(TAG,"onComplete "+hashCode());
    }
 
    protected abstract void onSuccess(RESULT data);
 
    protected void onFailed(HttpApiResult error){
        Log.i(TAG,"onFailed "+activity);
        if(activity != null){
            activity.showToast(error.msg);
        }
    }
 
}