简体   繁体   English

如何在模拟器中连接到本地主机

[英]How can I connect in emulator to the local host

I have wamp server in my windows. 我的窗户上有wamp server Also I have an android program that it should connect to my database and my php files.I created my database in wamp server and I saved my php files in www folder. 我也有一个Android程序,它应该连接到我的数据库和php文件。我在wamp server创建了数据库,并将php文件保存在www文件夹中。

Also I have an image in http://127.0.0.1/index/image.jpg folder in the wamp server and also I use http://127.0.0.1/index/image.jpg in source code.But when I run my program in emulator, It can not load the image from wamp server folder. 我在wamp server http://127.0.0.1/index/image.jpg文件夹中也有一个图像,并且在源代码中也使用了http://127.0.0.1/index/image.jpg 。但是当我运行模拟器中的程序,它无法从wamp server文件夹中加载图像。

I am pretty sure my code is correct.What is the solution? 我很确定我的代码是正确的,解决方案是什么?

To access your localhost server you'll have to use the special 10.0.2.2 ip. 要访问您的本地主机服务器,您必须使用特殊的10.0.2.2 ip。 You could also use the real ip of your machine on which the emulator is running on. 您还可以使用运行模拟器的计算机的真实IP。

More at http://developer.android.com/tools/devices/emulator.html#networkaddresses 有关更多信息,请访问http://developer.android.com/tools/devices/emulator.html#networkaddresses

You are connecting Android emulator to your localhost, you use 10.0.2.2 for connecting to localhost server. 您正在将Android模拟器连接到本地主机,并使用10.0.2.2连接到本地主机服务器。

as android emulator is acting as a device it has it own loopback interface which is 127.0.0.1 therefore it is inside android emulator. 由于android模拟器充当设备,它具有自己的环回接口,即127.0.0.1,因此它位于android模拟器内。 and it has its own ip address 10.0.2.15. 它有自己的IP地址10.0.2.15。 now that mean you need to connect to server running in your development machine from android emulator. 现在,这意味着您需要从android模拟器连接到开发计算机中运行的服务器。

reference https://developer.android.com/studio/run/emulator-commandline.html#networkaddresses 参考https://developer.android.com/studio/run/emulator-commandline.html#networkaddresses

If you trying to connect device to your pc through wi-fi - they should be in the same network, so ping command on device should return a value. 如果您尝试通过Wi-Fi将设备连接到PC,则它们应该位于同一网络中,因此设备上的ping命令应返回一个值。 Next you need to open port on your PC, just add a rule for your server in your firewall. 接下来,您需要打开PC上的端口,只需在防火墙中为服务器添加规则。

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

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