wjc
2025-02-25 8f14c06d68437bbef44df779ab27c9723d787267
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
package com.hdl.sdk.link.core.utils;
 
import android.text.TextUtils;
 
import com.google.gson.reflect.TypeToken;
import com.hdl.sdk.link.common.utils.LogUtils;
import com.hdl.sdk.link.common.utils.gson.GsonConvert;
 
import com.hdl.sdk.link.core.bean.LinkResponse;
import com.hdl.sdk.link.core.bean.response.BaseLocalResponse;
import com.hdl.sdk.link.core.bean.response.BaseLocalWithCodeResponse;
 
import java.lang.reflect.Type;
 
/**
 * Created by jlchen on 1/6/22.
 */
public class LinkResponseUtils<T> {
 
    /**
     * 使用真实的link协议
     */
    public static boolean userRealLink;
 
    public Type getType(){
       return new TypeToken<BaseLocalResponse<T>>() {}.getType();
    }
 
    /**
     * 转换提取LinkResponse里面的objects
     *
     * @param msg
     * @return
     */
    public static <T> T convertLinkResponse(Object msg, Type type) {
        T bean = null;
        if (msg != null && msg instanceof LinkResponse) {
            LinkResponse linkResponse = (LinkResponse) msg;
            String data = linkResponse.getData();
            if (!TextUtils.isEmpty(data)) {
                try {
                    final BaseLocalResponse<T> response = GsonConvert.getGson().fromJson(data, type);
                    if (response != null) {
                        bean = response.getObjects();
                    }
                } catch (Exception e) {
                    LogUtils.e("convertLinkResponse catch:" + e.getMessage());
                }
            }
        }
        return bean;
    }
 
    /**
     * 转换提取LinkResponse里面的objects  只有返回code   判断200是成功
     *
     * @param msg
     * @return
     */
    public static <T> T convertLinkWithCodeResponse(Object msg, Type type) {
        T bean = null;
        if (msg != null && msg instanceof LinkResponse) {
            LinkResponse linkResponse = (LinkResponse) msg;
            String data = linkResponse.getData();
            if (!TextUtils.isEmpty(data)) {
                try {
                    final BaseLocalWithCodeResponse<T> response = GsonConvert.getGson().fromJson(data, type);
                    if (response != null) {
                        bean = response.getCode();
                    }
                } catch (Exception e) {
                    LogUtils.e("convertLinkResponse catch:" + e.getMessage());
                }
            }
        }
        return bean;
    }
 
    public static boolean isUserRealLink() {
        return userRealLink;
    }
 
    /**
     * 使用真实的link协议
     * @param userRealLink
     */
    public static void setUserRealLink(boolean userRealLink) {
        LinkResponseUtils.userRealLink = userRealLink;
    }
}