user3165926
user3165926

Reputation: 93

Connecting to localhost via Android device

I'm trying to connect to my laptop's localhost via my android device. There's enough information on how to do this online, but for some reason it's not working for me.

I tried:

My laptop and phone are both connected to my router via wifi.

When trying to connect, my browser on my phone tells me that that it can't load the page because it's taking too long.

Edit:

I'm using Wampserver on my laptop

Upvotes: 0

Views: 3201

Answers (4)

Jahongir Murtozayev
Jahongir Murtozayev

Reputation: 437

After connecting with Wifi

  • Click Win+R and write cmd
  • After open Command line write "ipconfig" find value rus-"IPv4-адрес" eng-"IPv4-address" " IPv4-адрес. . . . . . . . . . . . : 192.168.1.100" for me
  • Come from "other divice" "192.168.1.100:Your Port"

Upvotes: 0

Michael
Michael

Reputation: 337

I work with PostgreSQL (Database) and Fedora 21 (Linux), for connection to db use jdbc driver.

This work for me:

  1. Connect android device to PC
  2. Connect your PC to hotspot (Phone wifi)
  3. Find ip address of PC by ip addr (see wifi connection wlp3s0 ipnet)
  4. Insert ip address to your code for example:

    Class.forName("org.postgresql.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:postgresql://ip address of PC:5432/dbname","username","pass");

  5. I work with PostgreSQL because that need to change pg_hba.conf file. host all all PC ip address/* [trust/md5]

  6. In postgresql.conf

    listen_addresses = '*'
    port = 5432
  1. restart PostgreSQL.
  2. stop PC firewall (in my case is systemctl stop firewall.service)
  3. It must work, enjoy.

Upvotes: 0

Syed Osama Maruf
Syed Osama Maruf

Reputation: 1935

If you want to access a url on your local machine say: http://localhost/mywebsite.com you need to give 10.0.2.2 instead of the localhost from your android device to access it. So your url in android will be like: http://10.0.2.2/mywebsite.com I have done this to access my websites hosted in IIS from the emulator though haven't checked it for a device. You can check if it helps :) P.S: The links are as an example not actual links.

Edit: The above wont work from device only emulator. For the device check: https://stackoverflow.com/a/8950423/4282901

Upvotes: 0

user3629714
user3629714

Reputation:

localhost or 127.0.0.1 won't work, because they only relate to the machine the server is running on, not any machine on the network.

You have to run ifconfig on your laptop and see which local ip your router registered it and use that one. Should be something like 192.168.Y.X - Y is usually 0 or 1, but depends on your router configuration.

So, to sum up: open terminal, run ifconfig if UNIX, ipconfig if windows and use the local IP displayed.

Upvotes: 0

Related Questions