|
14
|
+import java.util.Date;
|
|
13
|
15
|
import java.util.HashMap;
|
|
14
|
|
-import java.util.Random;
|
|
15
|
16
|
|
|
16
|
17
|
import ai.pai.lensman.BuildConfig;
|
|
17
|
18
|
import ai.pai.lensman.R;
|
|
|
|
@@ -30,7 +31,7 @@ public class SyncTimeInteractor implements BaseInteractor {
|
|
30
|
31
|
private SyncTimeListener listener;
|
|
31
|
32
|
private boolean isCancelled;
|
|
32
|
33
|
|
|
33
|
|
- public SyncTimeInteractor(String lensmanId, SyncTimeListener listener){
|
|
|
34
|
+ public SyncTimeInteractor(String lensmanId, SyncTimeListener listener) {
|
|
34
|
35
|
this.listener = listener;
|
|
35
|
36
|
this.lensmanId = lensmanId;
|
|
36
|
37
|
}
|
|
|
|
@@ -39,61 +40,60 @@ public class SyncTimeInteractor implements BaseInteractor {
|
|
39
|
40
|
public void startJob() {
|
|
40
|
41
|
cancelJob();
|
|
41
|
42
|
isCancelled = false;
|
|
42
|
|
- HashMap<String,String> params = new HashMap<>();
|
|
43
|
|
- params.put("user_id",lensmanId);
|
|
44
|
|
- if(BuildConfig.isTestMode){
|
|
45
|
|
- if(new Random().nextInt(100)%2==0){
|
|
46
|
|
- listener.onTimeSync(true,0);
|
|
47
|
|
- }else{
|
|
48
|
|
- listener.onTimeSync(false,R.string.sync_time_box_error);
|
|
49
|
|
- }
|
|
50
|
|
- }
|
|
51
|
|
- LogHelper.d("czy","开始时间同步任务");
|
|
52
|
|
- timeSyncTask = new HttpPostTask(params){
|
|
|
43
|
+ HashMap<String, String> params = new HashMap<>();
|
|
|
44
|
+ params.put("user_id", lensmanId);
|
|
|
45
|
+ LogHelper.d("czy", "开始时间同步任务");
|
|
|
46
|
+ timeSyncTask = new HttpPostTask(params) {
|
|
53
|
47
|
|
|
54
|
48
|
int msgId = R.string.sync_time_box_error;
|
|
55
|
49
|
|
|
56
|
50
|
@Override
|
|
57
|
51
|
protected boolean parseResponse(String response) {
|
|
58
|
|
- LogHelper.d("czy","时间同步任务 服务器应答="+response);
|
|
59
|
|
- try{
|
|
|
52
|
+ LogHelper.d("czy", "时间同步任务 服务器应答=" + response);
|
|
|
53
|
+ String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
|
|
54
|
+ try {
|
|
60
|
55
|
JSONObject json = new JSONObject(response);
|
|
61
|
56
|
int status = json.getInt("status");
|
|
62
|
|
- if(status == 200){
|
|
|
57
|
+ if (status == 200) {
|
|
63
|
58
|
JSONObject data = json.getJSONObject("data");
|
|
64
|
|
- String time = data.getString("time");
|
|
65
|
|
- HashMap<String,String> boxParams = new HashMap<>();
|
|
66
|
|
- boxParams.put("time",time);
|
|
67
|
|
- if(isCancelled){
|
|
68
|
|
- return false;
|
|
69
|
|
- }
|
|
70
|
|
- String setTimeResp = HttpUtils.doHttpPost(BoxUrlContainer.SET_TIME_URL,boxParams);
|
|
71
|
|
- LogHelper.d("czy","时间同步任务 box设置时间应答="+setTimeResp);
|
|
72
|
|
- if(isCancelled){
|
|
73
|
|
- return false;
|
|
74
|
|
- }
|
|
75
|
|
- SystemClock.sleep(1000);
|
|
76
|
|
- if(isCancelled){
|
|
77
|
|
- return false;
|
|
78
|
|
- }
|
|
79
|
|
- String boxResp = HttpUtils.doHttpPost(BoxUrlContainer.BOX_TIME_URL,null);
|
|
80
|
|
- LogHelper.d("czy","时间同步任务 box获取时间应答="+boxResp);
|
|
81
|
|
- if(isCancelled){
|
|
82
|
|
- return false;
|
|
83
|
|
- }
|
|
84
|
|
- JSONObject boxJson = new JSONObject(boxResp);
|
|
85
|
|
- int boxStatus = boxJson.getInt("status");
|
|
86
|
|
- if(boxStatus == 200){
|
|
87
|
|
- String boxTime = boxJson.getJSONObject("data").getString("time");
|
|
88
|
|
- if(time.substring(0,13).equals(boxTime.substring(0,13))){
|
|
89
|
|
- return true;
|
|
90
|
|
- }
|
|
|
59
|
+ time = data.getString("time");
|
|
|
60
|
+ }
|
|
|
61
|
+ } catch (Exception e) {
|
|
|
62
|
+ LogHelper.d("czy", "时间同步任务 服务器应答异常,采用本地时间 " + time);
|
|
|
63
|
+ }
|
|
|
64
|
+ try {
|
|
|
65
|
+ HashMap<String, String> boxParams = new HashMap<>();
|
|
|
66
|
+ boxParams.put("time", time);
|
|
|
67
|
+ if (isCancelled) {
|
|
|
68
|
+ return false;
|
|
|
69
|
+ }
|
|
|
70
|
+ String setTimeResp = HttpUtils.doHttpPost(BoxUrlContainer.SET_TIME_URL, boxParams);
|
|
|
71
|
+ LogHelper.d("czy", "时间同步任务 box设置时间应答=" + setTimeResp);
|
|
|
72
|
+ if (isCancelled) {
|
|
|
73
|
+ return false;
|
|
|
74
|
+ }
|
|
|
75
|
+ SystemClock.sleep(1000);
|
|
|
76
|
+ if (isCancelled) {
|
|
|
77
|
+ return false;
|
|
|
78
|
+ }
|
|
|
79
|
+ String boxResp = HttpUtils.doHttpPost(BoxUrlContainer.BOX_TIME_URL, null);
|
|
|
80
|
+ LogHelper.d("czy", "时间同步任务 box获取时间应答=" + boxResp);
|
|
|
81
|
+ if (isCancelled) {
|
|
|
82
|
+ return false;
|
|
|
83
|
+ }
|
|
|
84
|
+ if(BuildConfig.isTestMode){
|
|
|
85
|
+ return true;
|
|
|
86
|
+ }
|
|
|
87
|
+ JSONObject boxJson = new JSONObject(boxResp);
|
|
|
88
|
+ int boxStatus = boxJson.getInt("status");
|
|
|
89
|
+ if (boxStatus == 200) {
|
|
|
90
|
+ String boxTime = boxJson.getJSONObject("data").getString("time");
|
|
|
91
|
+ if (time.substring(0, 13).equals(boxTime.substring(0, 13))) {
|
|
|
92
|
+ return true;
|
|
91
|
93
|
}
|
|
92
|
|
- }else{
|
|
93
|
|
- msgId = R.string.sync_time_server_error;
|
|
94
|
94
|
}
|
|
95
|
|
- }catch (Exception e){
|
|
96
|
|
- LogHelper.e("czy","时间同步任务发生异常="+e);
|
|
|
95
|
+ } catch (Exception e) {
|
|
|
96
|
+ LogHelper.e("czy", "时间同步任务发生异常=" + e);
|
|
97
|
97
|
}
|
|
98
|
98
|
return false;
|
|
99
|
99
|
}
|
|
|
|
@@ -101,15 +101,15 @@ public class SyncTimeInteractor implements BaseInteractor {
|
|
101
|
101
|
@Override
|
|
102
|
102
|
protected void onPostFail() {
|
|
103
|
103
|
super.onPostFail();
|
|
104
|
|
- listener.onTimeSync(false,msgId);
|
|
105
|
|
- LogHelper.d("czy","时间同步失败");
|
|
|
104
|
+ listener.onTimeSync(false, msgId);
|
|
|
105
|
+ LogHelper.d("czy", "时间同步失败");
|
|
106
|
106
|
}
|
|
107
|
107
|
|
|
108
|
108
|
@Override
|
|
109
|
109
|
protected void onPostSuccess() {
|
|
110
|
110
|
super.onPostSuccess();
|
|
111
|
|
- listener.onTimeSync(true,0);
|
|
112
|
|
- LogHelper.d("czy","时间同步成功");
|
|
|
111
|
+ listener.onTimeSync(true, 0);
|
|
|
112
|
+ LogHelper.d("czy", "时间同步成功");
|
|
113
|
113
|
}
|
|
114
|
114
|
};
|
|
115
|
115
|
timeSyncTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.SERVER_TIME_URL);
|
|
|
|
@@ -117,18 +117,18 @@ public class SyncTimeInteractor implements BaseInteractor {
|
|
117
|
117
|
|
|
118
|
118
|
@Override
|
|
119
|
119
|
public void cancelJob() {
|
|
120
|
|
- if(timeSyncTask==null){
|
|
|
120
|
+ if (timeSyncTask == null) {
|
|
121
|
121
|
return;
|
|
122
|
122
|
}
|
|
123
|
|
- if(timeSyncTask.getStatus()== AsyncTask.Status.RUNNING){
|
|
|
123
|
+ if (timeSyncTask.getStatus() == AsyncTask.Status.RUNNING) {
|
|
124
|
124
|
timeSyncTask.cancel(true);
|
|
125
|
125
|
}
|
|
126
|
126
|
isCancelled = true;
|
|
127
|
127
|
timeSyncTask = null;
|
|
128
|
128
|
}
|
|
129
|
129
|
|
|
130
|
|
- public interface SyncTimeListener{
|
|
|
130
|
+ public interface SyncTimeListener {
|
|
131
|
131
|
|
|
132
|
|
- void onTimeSync(boolean result,@StringRes int strId);
|
|
|
132
|
+ void onTimeSync(boolean result, @StringRes int strId);
|
|
133
|
133
|
}
|
|
134
|
134
|
}
|
|
|
|
@@ -29,7 +29,7 @@ public class HttpPostTask extends AsyncTask<String,Integer,Boolean> {
|
|
29
|
29
|
rootUrl+="?platform=android";
|
|
30
|
30
|
}
|
|
31
|
31
|
rootUrl+="&version="+ BuildConfig.VERSION_NAME;
|
|
32
|
|
- rootUrl+="&channel="+"guanwang";
|
|
|
32
|
+ rootUrl+="&channel="+BuildConfig.FLAVOR;
|
|
33
|
33
|
String response= HttpUtils.doHttpPost(rootUrl,httpParams);
|
|
34
|
34
|
return parseResponse(response);
|
|
35
|
35
|
}
|
|
|
|
@@ -16,7 +16,7 @@ import java.util.Set;
|
|
16
|
16
|
public class HttpUtils {
|
|
17
|
17
|
|
|
18
|
18
|
private static final int READ_TIME_OUT = 20000;
|
|
19
|
|
- private static final int CONN_TIME_OUT = 20000;
|
|
|
19
|
+ private static final int CONN_TIME_OUT = 10000;
|
|
20
|
20
|
|
|
21
|
21
|
private static final String EQUAL_SIGN = "=";
|
|
22
|
22
|
private static final String CONN_SIGN = "&";
|