简体   繁体   English

通过Android设备连接到localhost

[英]Connecting to localhost via Android device

I'm trying to connect to my laptop's localhost via my android device. 我正试图通过我的Android设备连接到我的笔记本电脑的localhost。 There's enough information on how to do this online, but for some reason it's not working for me. 有关于如何在线完成此操作的足够信息,但由于某种原因,它不适合我。

I tried: 我试过了:

  • Setting an inbound rule in my firewall to allow incoming traffic on port 80 在我的防火墙中设置入站规则以允许端口80上的传入流量
  • Editing my httpd.conf file to allow from all instead of 127.0.0.1 编辑我的httpd.conf文件以允许全部而不是127.0.0.1

My laptop and phone are both connected to my router via wifi. 我的笔记本电脑和手机都通过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 我正在笔记本电脑上使用Wampserver

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. localhost或127.0.0.1将无法工作,因为它们只与运行服务器的计算机有关,而与网络上的任何计算机无关。

You have to run ifconfig on your laptop and see which local ip your router registered it and use that one. 您必须在笔记本电脑上运行ifconfig,并查看您的路由器注册了哪个本地IP并使用该IP。 Should be something like 192.168.YX - Y is usually 0 or 1, but depends on your router configuration. 应该像192.168.YX - Y通常是0或1,但取决于您的路由器配置。

So, to sum up: open terminal, run ifconfig if UNIX, ipconfig if windows and use the local IP displayed. 所以,总结一下:打开终端,运行ifconfig如果UNIX,ipconfig如果是windows并使用本地IP显示。

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. 如果要访问本地计算机上的URL,请说: http://localhost/mywebsite.com您需要从Android设备上提供10.0.2.2而不是localhost才能访问它。 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. 因此,你在android中的网址将是: http//10.0.2.2/mywebsite.com我这样做是为了从模拟器访问我在IIS中托管的网站,虽然没有检查它的设备。 You can check if it helps :) PS: The links are as an example not actual links. 您可以检查它是否有帮助:) PS:链接仅作为示例而非实际链接。

Edit : The above wont work from device only emulator. 编辑 :以上不会从仅设备模拟器工作。 For the device check: https://stackoverflow.com/a/8950423/4282901 对于设备检查: https//stackoverflow.com/a/8950423/4282901

I work with PostgreSQL (Database) and Fedora 21 (Linux), for connection to db use jdbc driver. 我使用PostgreSQL(数据库)和Fedora 21(Linux)连接到db use jdbc驱动程序。

This work for me: 这项工作对我来说:

  1. Connect android device to PC 将Android设备连接到PC
  2. Connect your PC to hotspot (Phone wifi) 将您的PC连接到热点(手机wifi)
  3. Find ip address of PC by ip addr (see wifi connection wlp3s0 ipnet ) 通过ip addr查找PC的ip addr (参见wifi连接wlp3s0 ipnet
  4. Insert ip address to your code for example: 将ip地址插入代码中,例如:

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

  5. I work with PostgreSQL because that need to change pg_hba.conf file. 我使用PostgreSQL是因为需要更改pg_hba.conf文件。 host all all PC ip address/* [trust/md5] 托管所有PC的IP地址/ * [trust / md5]

  6. In postgresql.conf 在postgresql.conf中

    listen_addresses = '*'
    port = 5432
  1. restart PostgreSQL. 重启PostgreSQL。
  2. stop PC firewall (in my case is systemctl stop firewall.service ) 停止PC防火墙(在我的例子中是systemctl stop firewall.service
  3. It must work, enjoy. 它必须工作,享受。

After connecting with Wifi 连接Wifi后

  • Click Win+R and write cmd 单击Win + R并编写cmd
  • After open Command line write "ipconfig" find value rus-"IPv4-адрес" eng-"IPv4-address" " IPv4-адрес. . . . . . . . . . . . : 192.168.1.100 " for me 打开命令行后写“ipconfig”找到值rus-“IPv4-адрес”eng-“IPv4-address”“IPv4-адрес... ... ...: 192.168.1.100
  • Come from "other divice" "192.168.1.100:Your Port" 来自“其他设备”“192.168.1.100:你的港口”

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

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