修复对获取服务器时间这个接口的解析问题

This commit is contained in:
chenjuntao
2018-12-03 14:59:16 +08:00
parent 22f68d098c
commit cd523bd552
7 changed files with 34 additions and 22 deletions

View File

@ -17,5 +17,9 @@ object AppExecutor {
override fun execute(command: Runnable) {
mainThreadHandler.post(command)
}
fun executeWithDelay(command: Runnable, delay: Long) {
mainThreadHandler.postDelayed(command, delay)
}
}
}

View File

@ -1,5 +1,7 @@
package com.gh.common.exposure.time
import com.gh.gamecenter.entity.TimeEntity
import com.gh.gamecenter.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.schedulers.Schedulers
@ -18,12 +20,16 @@ class Corrector {
fixedRateTimer("TimeUtil-Corrector-Checker", initialDelay = 0, period = TIME_CORRECTOR_ADJUST_PERIOD) {
RetrofitManager.getInstance(HaloApp.getInstance().application).api.time
.subscribeOn(Schedulers.io())
.subscribe({
// TODO Fix time parsing format.
val serverTime = java.lang.Long.parseLong(it.string())
delta = serverTime * 1000 - System.currentTimeMillis()
}, Throwable::printStackTrace)
.subscribe(object : Response<TimeEntity>() {
override fun onResponse(response: TimeEntity?) {
try {
val serverTime = response?.time!!
delta = serverTime * 1000 - System.currentTimeMillis()
} catch (e: NumberFormatException) {
e.printStackTrace()
}
}
})
}
}
}

View File

@ -4,8 +4,9 @@ import android.content.Context
import android.os.Environment
import android.preference.PreferenceManager
import com.gh.gamecenter.BuildConfig
import com.gh.gamecenter.entity.TimeEntity
import com.gh.gamecenter.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.gh.gamecenter.retrofit.StringResponse
import com.halo.assistant.HaloApp
import com.lightgame.utils.Util_System_Phone_State
import com.lightgame.utils.Utils
@ -26,18 +27,15 @@ object DeviceTokenUtils {
RetrofitManager.getInstance(context).api.time
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : StringResponse() {
override fun onResponse(response: String) {
if (response.matches("^[0-9]{10}$".toRegex())) {
try {
val editor = sp.edit()
editor.putLong("server_time", java.lang.Long.parseLong(response))
editor.putLong("client_time", System.currentTimeMillis() / 1000)
editor.apply()
} catch (e: NumberFormatException) {
e.printStackTrace()
}
.subscribe(object : Response<TimeEntity>() {
override fun onResponse(response: TimeEntity?) {
try {
val editor = sp.edit()
editor.putLong("server_time", response?.time!!)
editor.putLong("client_time", System.currentTimeMillis() / 1000)
editor.apply()
} catch (e: NumberFormatException) {
e.printStackTrace()
}
}
})

View File

@ -0,0 +1,3 @@
package com.gh.gamecenter.entity
data class TimeEntity(var time: Long)

View File

@ -33,6 +33,7 @@ import com.gh.gamecenter.entity.SubjectEntity;
import com.gh.gamecenter.entity.SubjectRecommendEntity;
import com.gh.gamecenter.entity.SubjectSettingEntity;
import com.gh.gamecenter.entity.TagEntity;
import com.gh.gamecenter.entity.TimeEntity;
import com.gh.gamecenter.entity.ToolBoxEntity;
import com.gh.gamecenter.entity.UserInfoEntity;
import com.gh.gamecenter.entity.VersionVoteEntity;
@ -121,7 +122,7 @@ public interface ApiService {
* 获取服务器时间
*/
@GET("time")
Observable<ResponseBody> getTime();
Observable<TimeEntity> getTime();
/**
* 统计下载量

View File

@ -106,7 +106,7 @@ public class HaloApp extends TinkerAppLike {
Log.e("CHANNEL_ID", mChannel);
// DataUtils是根据debug/release自动编译对应的文件的
AppExecutor.getUiExecutor().execute( () -> DataUtils.init(getApplication(), mChannel));
AppExecutor.getUiExecutor().execute(() -> DataUtils.init(getApplication(), mChannel));
// 注册回调以用于做各种统计
registerActivityLifecycleCallbacks(new GHActivityLifecycleCallbacksImpl());