admin
2021-07-20 27bd1f81221b8c8e8047118a64c2beb7bc214bbb
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.yeshi.base.utils.http;
 
import android.content.Context;
 
import com.lcjian.library.util.ManifestDataUtil;
import com.lcjian.library.util.common.PackageUtils2;
import com.lcjian.library.util.common.StringUtils;
import com.lcjian.library.util.security.MD5Utils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;
import com.loopj.android.http.SyncHttpClient;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
 
public class API {
 
    public static final boolean isDebug = false;
 
    private static final String TAG = "BeibeiVideoAPI";
 
    public static String ERROR_NOTICE = "";
 
    public static String HOST = "http://api.ysdq.yeshitv.com:8089";
 
    public static String BASE_URL = HOST + "/BuWan/api/v2/";
 
    private static AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
 
    private static SyncHttpClient syncHttpClient = new SyncHttpClient();
 
    static {
        asyncHttpClient.setTimeout(60 * 1000);
        syncHttpClient.setTimeout(60 * 1000);
        asyncHttpClient.setURLEncodingEnabled(false);
        syncHttpClient.setURLEncodingEnabled(false);
    }
 
    public static void postRequest(Context context, String url, LinkedHashMap<String, String> params,
                                   ResponseHandlerInterface handler) {
        commonPost(context, url, params, handler);
    }
 
 
    public static LinkedHashMap<String, String> validateParams(
            LinkedHashMap<String, String> params, Context context) {
        params.put("System", "1");
        StringBuilder sign = new StringBuilder();
        sign.append(params.get("Method"))
                .append(StringUtils.isEmpty(params.get("Uid")) ? params.get("Device")
                        : params.get("Uid")).append(params.get("System"));
        params.put("Sign", MD5Utils.getMD532(sign.toString()));
        params.put("Platform", "Android");
        params.put("Channel", ManifestDataUtil.getAppMetaData(context, "UMENG_CHANNEL"));
        return params;
    }
 
    @SuppressWarnings("unused")
    private static void commonGet(Context context, String url,
                                  LinkedHashMap<String, String> params,
                                  ResponseHandlerInterface handler) {
        commonGet(context, url, params, handler, true);
    }
 
    public static void commonGet(Context context, String url,
                                 LinkedHashMap<String, String> params,
                                 ResponseHandlerInterface handler, boolean asyn) {
        params.put("Package", context.getPackageName());
        LinkedHashMap<String, String> map = validateParams(params, context);
 
        RequestParams requestParams = new RequestParams(map);
        if (asyn) {
            (asyncHttpClient).get(context, url, requestParams, handler);
        } else {
            (syncHttpClient).get(context, url, requestParams, handler);
        }
    }
 
    public static void commonPost(Context context, String url,
                                  LinkedHashMap<String, String> params,
                                  ResponseHandlerInterface handler) {
        commonPost(context, url, params, null, handler);
    }
 
    public static void commonPost(Context context, String url,
                                   LinkedHashMap<String, String> params, HashMap<String, File> files,
                                   ResponseHandlerInterface handler) {
        commonPost(context, url, params, files, handler, true);
    }
 
    public static void commonPost(Context context, String url,
                                   LinkedHashMap<String, String> params, HashMap<String, File> files,
                                   ResponseHandlerInterface handler, boolean asyn) {
        params.put("Package", "com.mugua.mgvideo");//context.getPackageName()
        int version = PackageUtils2.getVersionCode(context);
        params.put("Version", version + "");
 
        LinkedHashMap<String, String> map = validateParams(params, context);
        RequestParams requestParams = new RequestParams(map);
 
        if (files != null) {
            for (Entry<String, File> entry : files.entrySet()) {
                try {
                    requestParams.put(entry.getKey(), entry.getValue());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        if (asyn) {
            // asyncHttpClient.setProxy("192.168.1.122", 8888);
            asyncHttpClient.post(context, url, requestParams, handler);
        } else {
            // asyncHttpClient.setProxy("192.168.1.122", 8888);
            syncHttpClient.post(context, url, requestParams, handler);
        }
    }
 
    public static void commonGet(Context context, String url,
                                 RequestParams params, ResponseHandlerInterface handler, boolean asyn) {
        if (asyn) {
            asyncHttpClient.get(context, url, params, handler);
        } else {
            syncHttpClient.get(context, url, params, handler);
        }
    }
 
    public static void commonGet(Context context, String url,
                                 ResponseHandlerInterface handler, boolean asyn) {
        if (asyn) {
            asyncHttpClient.get(context, url, handler);
        } else {
            syncHttpClient.get(context, url, handler);
        }
 
    }
}