Android connection to localhost

南笙酒味 提交于 2019-11-27 08:22:19

First you have to bind the IP address of the machine where your server is running in the eclipse settings.

You can do this like this.

Right click on the PHP project in the eclipse then Run Configuration then In the Web Application where you will find the Argument tab. Now here give the port and LAN IP address of your machine on which your server is running.

Something like this --port=8888 --address=192.168.1.6 then update the URL to http://192.168.1.6:8080/tests/PhpProject1/connectionBDD.php

Here in my case this is my LAN IP address 192.168.1.6, there you will have to find it using the network command like ipconfig , ifconfig and use that IP address.

IP-address 10.0.2.2 is used to fetch data from the emulator. Localhost will always point to the emulator/android device running the application. To let your device fetch data from your pc, it should be in the same network (connected by WiFi to your router) and you should use the local IP-address of your pc (normally a 192.168.1.x-number).

If you try to connect to "localhost", it will resolve to the Android device, not to your own localhost (unless you are running within the emulator). What I recommend for development is to add an overflow menu in the action bar that has an entry named "Settings" that provides a Settings activity for specifying application settings, and to have a "Developer options" entry in "Settings" that lets you specify a custom server address to use. During development, you can use this option to enter a custom server address for your app. (You will need a real server address that is actually reachable over the Internet rather than using localhost for this).

if you are using your phone instead of emulator and running services on localhost then in url instead of '10.0.2.2' use IP address of your PC.

One simple way i know is keep mobile data on and share wifi . Connect your laptop or computer to this wifi . Now see ip of ur laptop or desktop. Call service from ur phone . Since your phone and your computer are in same network now.

I assume you are trying to access web service available on your PC from either an android simulator or a real device.

For an android emulator, you must NOT just use "localhost", because "localhost" means android emulator itself, NOT the host PC.

you need modify the /etc/hosts file or the simulator or real device. add a line like "192.168.0.100 service.local".

I tried "10.0.2.2:80/mysitename/page.php" Miracle happened, it's working now. I am on Mac and using XAMPP for server.

You can change port no. to 80 and try. port 8080 was not working for me!

Cheers.

Just Install the "conveyor by Keyoti" the extension in Visual studio and it will generate a url according to your ip address automatically. here's the link:

conveyor

so far so good....!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!