wjc
2023-07-07 22494af577e21a930abef309f2f60c03c9615bd1
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
package com.hdl.linkpm.sdk.core.response;
 
 
import android.content.pm.ApplicationInfo;
import android.provider.Settings;
import android.widget.Toast;
 
import com.hdl.hdlhttp.callback.HxException;
import com.hdl.hdlhttp.response.GsonSubscribe;
import com.hdl.hdlhttp.utils.ParameterizedTypeImpl;
import com.hdl.linkpm.sdk.HDLLinkPMSdk;
import com.hdl.linkpm.sdk.core.code.HDLCloudCode;
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.linkpm.sdk.utils.HDLGsonUtils;
 
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
 
 
/**
 * Created by Tong on 2021/11/8.
 */
public abstract class HDLResponse<K> extends GsonSubscribe<BaseInfo<K>> {
 
    public abstract void onResponse(K response);
 
    public abstract void onFailure(HDLException e);
 
    @Override
    public void onNext(String s) {
        try {
            Type type = getClass().getGenericSuperclass();
            if (type instanceof ParameterizedType) {
                Type[] types = ((ParameterizedType) type).getActualTypeArguments();
                Type parameterizedType = new ParameterizedTypeImpl(BaseInfo.class, types, BaseInfo.class);
                BaseInfo<K> data = HDLGsonUtils.fromJson(s, parameterizedType);
                onSuccess(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.onError(HxException.handleException(e));
        }
    }
 
    @Override
    public final void onSuccess(BaseInfo<K> response) {
        if (response.getCode() == HDLCloudCode.SUCCEED) {
            onResponse(response.getData());
        } else {
            if(HDLLinkPMSdk.isDebugVersion()){
                HDLLinkPMSdk.showToast(response.getMessage());
            }
            onFailure(new HDLException(response.getCode(), response.getMessage()));
        }
    }
 
    @Override
    public final void onError(HxException e) {
        if(HDLLinkPMSdk.isDebugVersion()){
            HDLLinkPMSdk.showToast(e.getRawThrowable().getMessage());
        }
        onFailure(new HDLException(e.getCode(),e.getRawThrowable().getMessage()));
    }
 
}