2

I have an issue when I trying to call my web service by pointing to the machine in LAN.

After some search over SO I have found that I cant use following patterns

  1. http://localhost/..
  2. http://localhost:port/..
  3. http://127.0.0.1:port/..

Then I tried to access it by specifying my machine name like this.

  1. http://machine_name:port/....
  2. http://machine_name/....

Still it is not working from my android device. Can any one explain why I can do this? And Ho can I achieve this?

Can anyone make this idea clear? because I want to know the differences in accessing a local server machine from other machines in same LAN and from an android phone. :)

Deniz
  • 12,332
  • 10
  • 44
  • 62

4 Answers4

2

if you testing in emulator than you have to use

10.0.2.2

if you are using phone connected in same LAN then you have to use ip address of you server machine.

Imtiyaz Khalani
  • 2,037
  • 18
  • 32
1

Localhost and 127.0.0.1 point to the current machine you are requesting from. So when connecting to localhost or 127.0.0.1 on your Android, the request is sent to your android and never leaves as it is asking for a connection to itself (which it is most likely not setup for)

Therefore, to connect to your dev machine from a remote device, the simplest of ways is to find the IP address of your dev machine and attempt to use that as the address on your Android or any other remote device on your local network.

Sators
  • 2,746
  • 1
  • 20
  • 26
0

Every reference I've found has been incorrect... Lot's of people say to use your IPv4...That is incorrect, I tried a list of IP's multiple times

The problem for me took 2 days 8 hours each to solve. You need to make sure that your device is connected to the same network as your PC for me, this didn't work on my home network, I had to connect my PC and a 2nd Cell phone to the hotspot of my primary cell phone. Then I could be sure they were on the same network. This gave me the ability to link my phone with Android Studio via Wifi, but I ended up using USB to accomplish this task.

Next I found a reference on setting up adb and performed the command Command to Run after Downloading adb

Then to see my reverse porting I ran ipconfig on my windows command line where I could see that it worked.Image of success message

Next I set my base url as http://127.0.0.1:8080/api/endpoint

Running the command worked and my API call went through.

launghe
  • 11
  • 3
-1

Goto Command prompt, type ipconfig Then note down your ipv4 address. you will find something like 192.168.xxx.xxx put that address as url

Akshay Shinde
  • 945
  • 10
  • 23