当网络连接断开时,将响应保存到服务器 [英] Save Response to Server When Network Connection goes OFF

查看:82
本文介绍了当网络连接断开时,将响应保存到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序

客户端应用

服务器应用

在Android中

我想要什么

要在我的服务器应用程序中检查天气客户端应用程序是否具有互联网连接.

To check in my server app that weather client app has internet connection or not.

我做了什么

我已经阅读了此帖子

我已使用BroadcastReciever收听天气,互联网是否可用.一切都很好.当互联网连接正常时,我会将值在线保存到Firebase"true"

I have used BroadcastReciever to Listen weather internet is available or not. All is well. When internet connection goes right , i am saving value online to Firebase "true"

但是

互联网连接断开时,

我正在使用 Firebase onDisconnect()方法保存ServerValue.TIMESTAMP

它可以在两分钟内工作,但有时它不会更新Firebase,并且值保持为真.

It works sometime in two minute but sometime it doesn't update firebase and value remains true.

请注意在连接客户端应用程序时需要什么,在firebase上应保存为true,而在未连接时应保存为false.尽管在我的服务器应用中,我会检索那些值以显示给客户端在线还是离线

Note what i want when client app is connected ,on firebase it should save true and when it is not connected it should save false . Though in my server app i will retreive those values to show to client is online or offline

在Android中是否还有其他技术可以做到这一点?

is there any other technique to do such a scenario in android ?

您对我目前的情况有何建议??

What do you suggest any improvement in my current scenario. ?

我们将非常感谢您的帮助.

Help will highly appreciated.

谢谢

推荐答案

保留此 onDisconnect(),编写一个无能为力的API,只是在固定的时间后连续ping服务器,让我们例如,每隔2秒钟就会调用一次API(通过服务),因此,如果网络断开连接或手机甚至关机,由于API不会响应服务器,在这里,您将编写一个代码,以防应用程序执行该操作.2秒后(或5秒后可以说)连接到服务器,响应(联机状态)应自动为 FALSE

leave this onDisconnect(), write an API that will do nothing but will just ping the server after a fixed time continuously, let's say after each 2 seconds the API will be called(through service), so in case the net is disconnected or the cell phone is even off, since API will not respond to the server, here you will write a code in case app did't ping to the server after 2 seconds(or you can say after 5 seconds), the response(online status) should be FALSE automatically!

因此,如果应用程序再次连接到互联网,则由于该服务正在运行,因此该服务会将您的false再次更新为 TRUE 太简单了!

so in case the app is again connected to internet, since the service is running so service will update your false into TRUE again that's too simple!

我认为这是您必需的功能!

i think this is your required function!

这篇关于当网络连接断开时,将响应保存到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆