优化运行日志上传重传机制
This commit is contained in:
parent
0a20d978ef
commit
3f82f1198b
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -29,19 +29,4 @@ class HttpServer {
|
|||
server.listen(3389)
|
||||
Log.d(TAG, "run: httpServer start... listen 3389")
|
||||
}
|
||||
|
||||
private fun getJSONFromReq(req: AsyncHttpServerRequest): JSONObject? {
|
||||
var obj: JSONObject? = null
|
||||
val body = req.getBody<AsyncHttpRequestBody<*>>()
|
||||
if (body is JSONObjectBody) {
|
||||
obj = body.get()
|
||||
} else if (body is StringBody) {
|
||||
try {
|
||||
obj = JSONObject(body.get())
|
||||
} catch (e: JSONException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
return obj
|
||||
}
|
||||
}
|
|
@ -73,7 +73,7 @@ class TaskSocketServer(private val context: Context, port: Int) {
|
|||
try {
|
||||
//只保持一个链接,新的进来就把老的断开
|
||||
val socket = soc!!.accept()
|
||||
socket.soTimeout = 5000 * 5
|
||||
socket.soTimeout = 5000
|
||||
if (client != null) {
|
||||
client!!.close()
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@ import okhttp3.RequestBody
|
|||
import com.zhouyou.http.callback.SimpleCallBack
|
||||
import com.zhouyou.http.exception.ApiException
|
||||
import okhttp3.Request
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
|
||||
/**
|
||||
|
@ -29,49 +28,52 @@ import java.util.concurrent.CountDownLatch
|
|||
* @description:
|
||||
*/
|
||||
object EasyHttpTool {
|
||||
val TAG = this.javaClass.simpleName
|
||||
|
||||
/**
|
||||
* 提交运行时长
|
||||
* @param runtimeJson String
|
||||
*/
|
||||
lateinit var latch: CountDownLatch
|
||||
var isSendSuccess = false
|
||||
// lateinit var latch: CountDownLatch
|
||||
// var isSendSuccess = false
|
||||
fun sendRunTimeToServer(runtimeJson: String) {
|
||||
"sendRunTimeToServer run".logd()
|
||||
latch = CountDownLatch(1)
|
||||
// latch = CountDownLatch(1)
|
||||
sendRuntimeToServerRequest(runtimeJson)
|
||||
Thread {
|
||||
latch.await()
|
||||
var count = 10
|
||||
// 网络可用时每一分钟发一次请求
|
||||
while (!isSendSuccess && count > 0) {
|
||||
latch = CountDownLatch(1)
|
||||
sendRuntimeToServerRequest(runtimeJson)
|
||||
Log.d("EasyHttpTool", "sendRunTimeToServer: 重发次数${count}")
|
||||
latch.await()
|
||||
count--;
|
||||
Thread.sleep(1000 * 60)
|
||||
}
|
||||
// 网络不可用时监听网络状态是否发送请求
|
||||
if (!isSendSuccess) {
|
||||
var isRunning = true
|
||||
while (isRunning) {
|
||||
val networkAvailable = NetworkChangeReceiver.isNetworkAvailable
|
||||
if (networkAvailable) {
|
||||
latch = CountDownLatch(1)
|
||||
sendRuntimeToServerRequest(runtimeJson)
|
||||
Log.d("EasyHttpTool", "sendRunTimeToServer: 重发网络${networkAvailable}")
|
||||
latch.await()
|
||||
if (isSendSuccess) {
|
||||
isRunning = false
|
||||
}
|
||||
}
|
||||
Thread.sleep(1000 * 60)
|
||||
}
|
||||
}
|
||||
}.start()
|
||||
|
||||
// Thread {
|
||||
// latch.await()
|
||||
// var count = 10
|
||||
// // 网络可用时每一分钟发一次请求
|
||||
// while (!isSendSuccess && count > 0) {
|
||||
// latch = CountDownLatch(1)
|
||||
// sendRuntimeToServerRequest(runtimeJson)
|
||||
// Log.d("EasyHttpTool", "sendRunTimeToServer: 重发次数${count}")
|
||||
// latch.await()
|
||||
// count--;
|
||||
// Thread.sleep(1000 * 60)
|
||||
// }
|
||||
// // 网络不可用时监听网络状态是否发送请求
|
||||
// if (!isSendSuccess) {
|
||||
// var isRunning = true
|
||||
// while (isRunning) {
|
||||
// val networkAvailable = NetworkChangeReceiver.isNetworkAvailable
|
||||
// if (networkAvailable) {
|
||||
// latch = CountDownLatch(1)
|
||||
// sendRuntimeToServerRequest(runtimeJson)
|
||||
// Log.d("EasyHttpTool", "sendRunTimeToServer: 重发网络${networkAvailable}")
|
||||
// latch.await()
|
||||
// if (isSendSuccess) {
|
||||
// isRunning = false
|
||||
// }
|
||||
// }
|
||||
// Thread.sleep(1000 * 60)
|
||||
// }
|
||||
// }
|
||||
// }.start()
|
||||
}
|
||||
|
||||
|
||||
fun sendRuntimeToServerRequest(runtimeJson: String) {
|
||||
Log.d("EasyHttpTool", "sendRuntimeToServerRequest: ")
|
||||
val parse = ("application/json;charset=UTF-8").toMediaTypeOrNull()
|
||||
|
@ -93,14 +95,14 @@ object EasyHttpTool {
|
|||
.execute(object : SimpleCallBack<String>() {
|
||||
override fun onError(e: ApiException?) {
|
||||
"上传运行时长失败:${e?.message}".loge()
|
||||
isSendSuccess = false
|
||||
latch.countDown()
|
||||
// isSendSuccess = false
|
||||
// latch.countDown()
|
||||
}
|
||||
|
||||
override fun onSuccess(t: String?) {
|
||||
"上传运行时长成功:${t}".loge()
|
||||
isSendSuccess = true
|
||||
latch.countDown()
|
||||
// isSendSuccess = true
|
||||
// latch.countDown()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -109,16 +111,51 @@ object EasyHttpTool {
|
|||
/**
|
||||
* 提交运行时长
|
||||
*/
|
||||
private const val retryInterval: Long = 1000 * 60
|
||||
// private const val retryInterval: Long = 1000
|
||||
fun sendRunTimeToServer() {
|
||||
val sumTimeList = DaoUtil.getRunTimeDao().queryBuilder().where(
|
||||
RunTimeDataDao.Properties.IsUpload.eq(0)
|
||||
).list()
|
||||
"未提交的运行时长数量sumTimeList size: ${sumTimeList?.size}".logd()
|
||||
// "未提交的运行时长数量sumTimeList size: ${sumTimeList?.size}".logd()
|
||||
Log.d(TAG, "sendRunTimeToServer: 未提交的运行时长数量: ${sumTimeList?.size}")
|
||||
if (!sumTimeList.isNullOrEmpty()) {
|
||||
|
||||
for (runTimeData in sumTimeList) {
|
||||
|
||||
sendRunLogToServer(runTimeData)
|
||||
Thread {
|
||||
var isSendSuccess = sendRunLogToServer(runTimeData)
|
||||
var count = 10
|
||||
// 是否需要重发
|
||||
// 网络可用时每一分钟发一次请求
|
||||
while (!isSendSuccess && count > 0) {
|
||||
isSendSuccess = sendRunLogToServer(runTimeData)
|
||||
Log.d(TAG, "sendRunTimeToServer: 重发次数${11 - count}")
|
||||
if (isSendSuccess) {
|
||||
Log.d(TAG, "sendRunTimeToServer: 发送成功 重发次数${11 - count}")
|
||||
}
|
||||
count--;
|
||||
Thread.sleep(retryInterval)
|
||||
}
|
||||
// 网络不可用时监听网络状态是否发送请求
|
||||
if (!isSendSuccess) {
|
||||
var isRunning = true
|
||||
while (isRunning) {
|
||||
var available = NetworkChangeReceiver.isNetworkAvailable
|
||||
Log.d(TAG, "sendRunTimeToServer: 重发网络$available")
|
||||
if (available) {
|
||||
isSendSuccess = sendRunLogToServer(runTimeData)
|
||||
if (isSendSuccess) {
|
||||
isRunning = false
|
||||
Log.d(TAG, "sendRunTimeToServer: 发送成功重发网络$available")
|
||||
}
|
||||
}
|
||||
Thread.sleep(retryInterval)
|
||||
}
|
||||
}
|
||||
Log.d(
|
||||
"EasyHttpTool",
|
||||
"sendRunTimeToServer: 运行日志发送成功${runTimeData.cardId}"
|
||||
)
|
||||
}.start()
|
||||
|
||||
// val runtimeJson = RuntimeJson(runTimeData.cardId,runTimeData.sumRuntime,runTimeData.startRuntime)
|
||||
// val runtimeJsonStr = Gson().toJson(runtimeJson)
|
||||
|
@ -163,7 +200,7 @@ object EasyHttpTool {
|
|||
/**
|
||||
* 上传1条运行日志
|
||||
*/
|
||||
fun sendRunLogToServer(runTimeData: RunTimeData) {
|
||||
fun sendRunLogToServer(runTimeData: RunTimeData): Boolean {
|
||||
// val logJson = ProcessingCommands.gson.toJson(dbPlayerLogs)
|
||||
val runtimeJson =
|
||||
RuntimeJson(runTimeData.cardId, runTimeData.sumRuntime, runTimeData.startRuntime)
|
||||
|
@ -191,11 +228,14 @@ object EasyHttpTool {
|
|||
runTimeData.isUpload = 1
|
||||
DaoUtil.getRunTimeDao().update(runTimeData)
|
||||
"将提交的runTimeData标记成已上传".logd()
|
||||
return true
|
||||
} else {
|
||||
"上传运行时长失败".loge()
|
||||
return false
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
"上传运行时长失败:${e?.message}".loge()
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<resources>
|
||||
<string name="app_name">TaxiApp</string>
|
||||
<!-- <string name="app_name">VehiclePlayer</string>-->
|
||||
<!-- <string name="app_name">TaxiApp</string>-->
|
||||
<string name="app_name">VehiclePlayer</string>
|
||||
<string name="title_activity_top_level">TopLevelActivity</string>
|
||||
|
||||
<!-- <string name="media_resource_base">android.resource://net.sysolution.taxiapp/</string>-->
|
||||
|
|
Loading…
Reference in New Issue
Block a user