当网络可用时自动连接到互联网
我必须开发一个应用程序,它收集一些数据(从数据库或平面文件或其他东西)并连接到作为存储库的服务器并将数据存储在其中。
功能要求是:
- 客户端应用程序必须检测可用网络
- 连接到网络自动
- 要求用户进行身份验证...例如:互联网
任何人请建议我应该使用什么API。
用java可以吗?
I have to develop an application which collects some data(from databases or flat files or something else) and connect to a server which is a repository and store the data in it.
The functional requirement is:
- client application must detect available network
- connect to network automatically
- ask user for authentication ... for eg: internet
Any one please suggest me what API I should use.
Is it possible using java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只是假设您已连接到互联网并尝试连接。如果失败,请稍等一下,然后重试。令人作呕。
即您不需要知道互联网是否可用即可尝试使用它。
I would just assume you are connected to the internet and try to connect anyway. If it fails, wait a bit and try again. ad nauseum.
i.e. You don't need to know if the internet is available to try and use it.
要回答“互联网可用性”问题,我建议您使用 apache HttpClient 并简单地尝试从已知的 URL(例如
www.google.com
)或什至与您的特定任务相关的 URL 中获取内容。如果成功,那么您就知道您已连接到互联网。确保使用较低的连接超时,以便在连接不存在时不会完全阻止应用程序。
如果您不使用 HttpClient 而使用普通 Java,则使用 cryptic:
10 秒设置超时来确定是否有连接应该足够了。否则它可能会永远阻塞。
To answer the "internet availability" issue I'd suggest you use apache HttpClient and simply try fetching content from a known URL, e.g
www.google.com
or even a URL related to the specific task of yours. If it succeeds then you know that you've got internet connectivity.Make sure you use a low connect timeout in order to not completelly block the application in case connectivity is not there.
If you don't use HttpClient but plain Java then set the timeout using the cryptic:
10 seconds to determine whether you have connection or not should be enough. Otherwise it could block forever.
了解网络或任何其他资源是否可用的最佳方法是尝试使用它。你实际上无法预测未来;-) 只是尝试建立你需要建立的联系,并处理失败。
The best way to know the network, or any other resource, is available is to try to use it. You can't actually foretell the future ;-) Just try to make the connection you need to make, and handle the failures.