简体   繁体   English

Android连接到本地Java ServerSocket?

[英]Android connecting to local java ServerSocket?

I have written a basic java http server using sockets. 我用套接字编写了一个基本的java http服务器。 It has simple REST interface and responds to GET/POST requests. 它具有简单的REST接口并响应GET / POST请求。 I set out to do a small android app connecting to this interface. 我着手做一个连接到此界面的小型android应用。 I can connect to my servers using browsers from my pc and also I tried out from vmware player virtual machines ubuntu and xp. 我可以使用PC上的浏览器连接到服务器,也可以从vmware player虚拟机ubuntu和xp中进行尝试。 All work well. 一切顺利。 I was afraid of testing on emulator so deployed the app on android phone. 我害怕在模拟器上进行测试,因此将应用程序部署在android手机上。 It cannot connect to the java servers. 它无法连接到Java服务器。 Both device and PC is connected to same network. 设备和PC均连接到同一网络。 Am I missing something here like port forwarding etc ? 我在这里缺少端口转发之类的东西吗? If that's the problem how come I was able to connect from my virtual machines? 如果这是问题,那么我如何能够从虚拟机进行连接?

Any help/suggestions are appreciated. 任何帮助/建议表示赞赏。

Regards, Lalith 此致,Lalith

Can you have forgotten to set the internet permission perhaps? 您是否忘记了设置互联网许可? You need this in your android manifest. 您需要在Android清单中使用它。

<uses-permission android:name="android.permission.INTERNET" />

/Stefan /斯特凡

Can you connect to this server from another PC running on the same network? 您可以从运行在同一网络上的另一台PC连接到此服务器吗? (This is not the same as connecting from a virtual machine running on the same box as the server) (这与从与服务器在同一个盒子上运行的虚拟机连接不同)

If not, you may have a firewall issue on the PC. 如果没有,您可能在PC上遇到防火墙问题。

You should test on the android emulator too, use the alias for the host loopback interface address that you can find in the emulator docs. 您也应该在Android模拟器上进行测试,使用您可以在模拟器文档中找到的主机环回接口地址的别名。

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

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