维护持久的http连接
我的 Android 应用程序每 10 秒向 servlet 发送一次数据。 Servlet 接收第一个请求并做出响应。但 servlet 在接下来的 10 秒后没有收到客户端发送的第二组数据。有人可以告诉我我该怎么做吗?与会话有关吗?
My android app sends data to a servlet every 10 seconds. the servlet receives the very first request and responds back. but the servlet doesn't receive the second set of data that the client sends after the next 10 seconds. could some one please tel me how do i go about doing this. is it something related to session?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HTTP 不是持久连接协议。您应该考虑为需要发送的每组数据发出一个 http 请求。
如果持久连接是强制性的(但我真的不知道是什么会迫使你这样做),你必须使用 TCP 协议......并且你将无法在服务器端使用 servlet而是侦听特定 TCP 端口的特定应用程序。
Http is not a persistent connection protocol. You should consider issuing one http request for each set of data you need to send.
If a persistent connection is mandatory (but I don't really see what would force you to do that), you have to work with the TCP protocol... and you won't be able to use a servlet on the server-side but a specific application listening to a specific TCP port.
这听起来很像您重用现有的
URLConnection
,而不是为每个请求创建一个新的 URLConnection,并且您通过空 catch 块来抑制异常,并且/或忽略stderr。对于每个独立请求,您必须创建一个新
URLConnection
。仅当 servlet 在
HttpSession
中存储您希望在后续请求中重新访问的内容时,会话管理才会出现。这里的情况似乎并非如此。This sounds very much like as if you're reusing an existing
URLConnection
instead of creating a new one for each request and that you're suppressing the exceptions by empty catch blocks and/or ignoring the stderr.For each independent request, you have to create a new
URLConnection
.The session management only comes into picture when the servlet is storing something in the
HttpSession
which you would like to re-access in the subsequent requests. This doesn't seem to be the case here.