简体   繁体   English

Android客户端-Java服务器应用程序

[英]Android Client- Java Server application

I have been working on a client server application the last few days on Android. 最近几天,我一直在Android上开发客户端服务器应用程序。 I choose to use Java socket communication, since i am familiar to it and it seemed easier at the time. 我选择使用Java套接字通信,因为我对此很熟悉,并且在当时似乎更容易。 I would like to help me though, cos i think that this is not enough for what i want to do. 我想为我提供帮助,因为我认为这还不足以实现我的目标。

At the time, as a basic demo application, i am using AsyncTask to connect to my Java Server and exchange text messages in order to arrange an appointment.This is the final goal, an Android app that can be used to book appointments. 当时,作为一个基本的演示应用程序,我正在使用AsyncTask连接到我的Java Server并交换文本消息以安排约会。这是最终目标,这是一个可用于预订约会的Android应用程序。

I have read that AsyncTask should dont be used for longer period of times, and i would really like the server to sent a message to the client when the time of the appointment is within the next hour or something. 我已经读过AsyncTask不应长时间使用,并且我真的希望服务器在约会时间在下一个小时之内或其他时间向客户端发送消息。

Can i do this using AsyncTask? 我可以使用AsyncTask吗? I think not. 我想不是。 From what i have read on the web, AsyncTask freezes the UI till it is done.(that i dont get it, since it is a thread, but anyhow thats what i read). 根据我在网上阅读的内容,AsyncTask会冻结UI直到完成为止。(我没有得到它,因为它是一个线程,但无论如何我都是这样)。

Would you recomend me using something else than sockets and AsyncTask? 您会推荐我使用套接字和AsyncTask之外的其他工具吗?

Thanks in advance,ilias 预先感谢,ilias

The problem of having a client waiting for a server's message is well known. 客户端等待服务器消息的问题是众所周知的。 You are right, keeping the connection open while waiting for a response is not a good idea. 没错,在等待响应时保持连接打开不是一个好主意。

The classical solution is polling. 经典的解决方案是轮询。 Choose a time interval, connect to the server, ask kindly if the result is already available. 选择一个时间间隔,连接到服务器,请询问结果是否已经可用。 If yes, get it and leave. 如果是,得到它并离开。 If no, leave and try again later. 如果否,请离开并稍后再试。 Of course this is no perfect solution, but many applications are implemented that way. 当然,这不是完美的解决方案,但是许多应用程序都是通过这种方式实现的。

For the interested reader: Have a look at Web Sockets . 对于感兴趣的读者:看看Web Sockets The idea is to keep alive a lightweight connection that enables the server to send data without explicit requests of the client. 这个想法是保持一个轻量级的连接,该连接使服务器能够发送数据而无需客户端的明确请求。 But this seems to be still work in progress. 但这似乎仍在进行中。

An async task is fine for what you are doing. 异步任务适合您的工作。 I think what you were reading about were applications that need to continuously poll for information from a server like a chat client. 我认为您正在阅读的应用程序需要不断地从服务器(如聊天客户端)中轮询信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM