简体   繁体   English

通过wifi将Android客户端连接到Java servlet

[英]Connecting Android client to a Java servlet via wifi

I am following this tutorial to implement this: http://www.veereshr.com/Android/AndroidToServlet 我正在按照本教程来实施: http : //www.veereshr.com/Android/AndroidToServlet

I got my server up and running and I am able to open its localhost link in my browser. 我已启动服务器并运行它,并且可以在浏览器中打开其localhost链接。 However, when I run the Android app on my Nexus 5, I get this error: 但是,当我在Nexus 5上运行Android应用时,出现以下错误:

11-26 01:34:46.501 1092-1340/com.example.nav.wificlient D/Exception:java.net.ConnectException: failed to connect to /10.0.2.2 (port 8080): connect failed: EHOSTUNREACH (No route to host) 11-26 01:34:46.501 1092-1340 / com.example.nav.wificlient D / Exception:java.net.ConnectException:无法连接到/10.0.2.2(端口8080):连接失败:EHOSTUNREACH(无路由主办)

11-26 01:35:29.171 1092-1557/com.example.nav.wificlient D/Exception: java.net.ConnectException: failed to connect to /10.0.2.2 (port 8080): connect failed: ETIMEDOUT (Connection timed out) 11-26 01:35:29.171 1092-1557 / com.example.nav.wificlient D / Exception:java.net.ConnectException:无法连接到/10.0.2.2(端口8080):连接失败:ETIMEDOUT(连接超时)

Both my laptop (which is running the server program) and my phone are connected to the same network (called eduroam) on a college campus. 我的笔记本电脑(正在运行服务器程序)和手机都连接到大学校园中的同一网络(称为eduroam)。 I made sure that my firewall settings are not blocking incoming connections on my laptop. 我确保防火墙设置不会阻止笔记本电脑上的传入连接。

I'm wondering if maybe the campus network settings are blocking this somehow...but I'm not sure. 我想知道校园网络设置是否可能以某种方式阻止了此操作...但是我不确定。 Any ideas on how I can narrow down what may be going wrong? 关于如何缩小可能出问题的任何想法?

10.0.2.2 is a unique address directly interpreted by the emulator to reference the machine running the emulator. 10.0.2.2是由仿真器直接解释为引用运行仿真器的计算机的唯一地址。 This is useful as "localhost" would simply reference the emulator itself. 这很有用,因为“ localhost”将仅引用模拟器本身。

As you are not running your android application on an emulator, but rather an actual device, you need to change your application so that, instead of connecting to 10.0.2.2, it connects to the local IP of the computer hosting the server. 由于您不是在模拟器上运行android应用程序,而是在实际设备上运行,因此您需要更改应用程序,以便与其连接到10.0.2.2而不是连接到托管服务器的计算机的本地IP。

See here for more information. 有关更多信息,请参见此处

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

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