gpt4 book ai didi

android - Android 中的发送超时重试模式

转载 作者:行者123 更新时间:2023-11-29 01:45:16 25 4
gpt4 key购买 nike

我想创建一个小应用程序,用于将一些 JSON 数据发送到远程服务器。

我需要处理用户没有互联网或数据无法发送等情况,因此手机会重试,直到数据已从手机发送并被服务器接收。

有这样的模式吗?通用模式还是特定于 Android 的模式?

提前致谢!

最佳答案

我正在使用超时时间戳来了解是否未发送消息(在本例中为串行通信):

private TimerTask WriterTask = new TimerTask() {
@Override
public void run() {
wasStarted = true;
synchronized (MessageQueue) {
if (mSIOManager != null && mSIOManager.getmWriteBufferSize() == 0
&& MessageQueue.size() > 0) {
QueueEntry item = MessageQueue.peek();
if (item != null && !item.sent) {
timeoutTimer = System.currentTimeMillis();
mSIOManager.writeAsync(item.Msg.getBytes());
Log.v(TAG, HexDump.dumpHexString(item.Msg.getBytes())
+ " - Written to Buffer");
item.sent = true;
}
}
try {
if (System.currentTimeMillis() - timeoutTimer > TIMEOUT && MessageQueue.peek().sent) {
Log.i(TAG, "Message timed out: " + HexDump.dumpHexString(MessageQueue.poll().Msg.getBytes()));
}
} catch (NullPointerException e) {
// Queue empty
}
}
}
};

因此,除了记录事件之外,您还可以从 ie 中增加任务的调度率。 100 到 10000 毫秒

Timer mTimer = new Timer();
mTimer.scheduleAtFixedRate(WriterTask,100L,10000L);

如果您的数据最终发送完毕,您只需取消计划

mTimer.cancel();

关于android - Android 中的发送超时重试模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21875092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com