从 J2ME 客户端轮询 HTTP 服务器
我的手机(客户端)上运行着一个 J2ME 应用程序,
我想打开与服务器的 HTTP 连接并不断轮询服务器上的更新信息。
执行的每次轮询都会耗尽 GPRS 字节,并且从长远来看会变得昂贵,因为 GPRS 计费是基于发送和接收的数据包。 是否有使用 HTTP 协议进行轮询的字节有效方式?
我也听说过长轮询,但我不确定它是如何工作的以及它的效率如何。
实际上,首选的方法是服务器告诉手机应用程序新数据已准备好使用,这样就不需要进行轮询,但是我不知道这些技术,尤其是在 J2ME 中。
I have a J2ME app running on my mobile phone(client),
I would like to open an HTTP connection with the server and keep polling for updated information on the server.
Every poll performed will use up GPRS bytes and would turn out expensive in the long run, as GPRS billing is based on packets sent and received.
Is there a byte efficient way of polling using the HTTP protocol?.
I have also heard of long polling, But I am not sure how it works and how efficient it would be.
Actually the preffered way would be for the Server to tell the phone app that new data is ready to be used that way polling won't be needed to be done, however I don't know of these techniques especially in J2ME.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想仅使用 HTTP 来解决此问题,长轮询将是最好的方法。 这相当容易。 首先,您需要在服务器端设置一个用于通知的 URL(例如
http://example.com/notify
),并定义通知协议。 该协议可以像一些文本行一样简单,每一行都是一个事件。 例如,手机上的轮询线程的工作方式如下:
您必须注意以下实现细节,
有人告诉我还有其他更有效的方法可以将通知推送到手机,例如使用短信和一些 IP 级别的技巧。 但您要么必须进行一些低级的不可移植编程,要么面临专利侵权的风险。 长轮询可能是使用纯 HTTP 解决方案可以获得的最佳方案。
If you want solve this problem using HTTP only, long polling would be the best way. It's fairly easy. First you need to setup an URL on server side for notification (e.g.
http://example.com/notify
), and define a notification protocol. The protocol can be as simply as some text lines and each line is an event. For example,The polling thread on the phone works like this,
You have to pay attention to following implementation details,
I was told there are other more efficient ways to push notifications to the phone, like using SMS and some IP-level tricks. But you either have to do some low level non-portable programming or run into risks of patent violations. Long polling is probably the best you can get with a HTTP only solution.
我不知道你所说的“轮询”是什么意思,你的意思是像 IMAP IDLE?
连接保持打开状态,并且一次又一次地建立连接本身没有开销。 如前所述,另一种可能的解决方案是 HTTP 请求的 HEAD 标头(忘记了,谢谢!)。
查看此教程,了解 J2ME 中 HTTP 连接的基础知识。
无法将数据推送到没有推送支持的应用程序/设备(例如黑莓)。
I don't know exactly what you mean by "polling", do you mean something like IMAP IDLE?
A connection stays open and there is no overhead for building up the connection itself again and again. As stated, another possible solution is the HEAD Header of a HTTP Request (forgot it, thanks!).
Look into this tutorial for the basic of HTTP Connections in J2ME.
Pushing data to an application/device without Push Support (like a Blackberry) is not possible.
最好的方法是使用套接字连接。 许多应用程序(例如 GMail)都使用它们。
The best way to do this is to use socket connection. Many application like GMail use them.
HEAD HTTP 请求 是 HTTP 提供的方法,如果您想检查页面是否无论是否更改,浏览器和代理服务器都使用它来检查页面是否已更新,而不消耗太多带宽。
在 HTTP 术语中,HEAD 请求与没有正文的 GET 相同,我认为最多只有几百个字节,如果您的轮询不是很频繁,那么这看起来是可以接受的。
The HEAD HTTP request is the method that HTTP provides if you want to check if a page has changed or not, it is used by browsers and proxy servers to check whether a page has been updated or not without consuming much bandwidth.
In HTTP terms, the HEAD request is the same as GET without the body, I assume this would be only a couple hundred bytes at most which looks acceptable if your polls are not very frequent.