简体   繁体   English

如何在Android设备上访问本地主机

[英]how to access localhost on android device

I have Spring MVC web application which I want to connect with mine android application in order to use the rest services. 我有Spring MVC Web应用程序,我想与我的android应用程序连接以便使用其余的服务。 I am working with my android phone, ALCATEL ONE TOUCH POP D5, but I didn't succeeded to connect it to access the localhost. 我正在使用我的Android手机ALCATEL ONE TOUCH POP D5,但是我没有成功连接它以访问本地主机。 I tried with finding my ip address with ipconfig and type it in the browser of the phone, but it doesn't show anything. 我尝试使用ipconfig查找我的ip地址,然后在手机的浏览器中键入它,但没有显示任何内容。 I also, enabled port 80 in Windows Firewall. 我也启用了Windows防火墙中的端口80。 I tried with usb tethering, also failed. 我尝试使用USB网络共享,也失败了。

My laptop and my android device are on the same network, and I use the phone's network with Wi-Fi tethering. 我的笔记本电脑和android设备位于同一网络上,并且我通过Wi-Fi网络共享使用手机的网络。

I would like someone to explain me, the whole scenario about connecting the android device to localhost? 我想有人向我解释一下,有关将android设备连接到本地主机的整个场景?

I tryed the solutions from various questions, but they don't seem to work for me, or I am skipping something. 我尝试了各种问题的解决方案,但它们似乎不适用于我,或者我正在跳过某些内容。 Here are the solutions I tried: How can I access my localhost through Android phone? 这是我尝试的解决方案: 如何通过Android手机访问本地主机? Accessing localhost of PC from USB connected Android mobile device 从USB连接的Android移动设备访问PC的本地主机

USB doesn't provide network to mobile device. USB不为移动设备提供网络。 If it's connected to your wifi, then hit your laptop address provided by the router. 如果已连接到您的wifi,则请点击路由器提供的笔记本电脑地址。 If it's connected to your mobile network, then first find out your router external IP address, then forward some port to that 10.0.2.2:portno and finally you'll be able to see that server from your device. 如果已连接到您的移动网络,则首先找出路由器的外部IP地址,然后将某个端口转发到该10.0.2.2:portno,最后您将能够从设备中看到该服务器。

I generally use the following approach : 我通常使用以下方法:

1) Run the web server on local ip of computer on network rather than localhost . 1)在网络上计算机的本地ip而不是localhost上运行Web服务器。 In python to accomplish this I run ipconfig in terminal and then do python manage.py runserver [ip]:8000 where ip is ip of computer on network from ipconfig.You can find similar thing for your framework. 在python中,为了做到这一点,我在终端中运行ipconfig,然后执行python manage.py runserver [ip]:8000,其中ip是从ipconfig从网络上获取计算机的IP。您可以在框架中找到类似的内容。

See this Link for above implementation :- 请参阅此链接以获取以上实现:-

Serve Django project on local WiFi Network 在本地WiFi网络上服务Django项目

2) Then when I hit that IP from any device on same network . 2)然后,当我从同一网络上的任何设备访问该IP时。 I could see the web application running . 我可以看到该Web应用程序正在运行。 So same goes for android application . Android应用程序也是如此。 You can access it as normal URL in android. 您可以在android中作为普通URL访问它。

I used the local network IP-addresses like 192.168.0.* to communicate with the server from my smartphone. 我使用本地网络IP地址(如192.168.0.*通过智能手机与服务器进行通信。 Both devices must be in the network of course 当然,两个设备都必须在网络中

This is what i do: 这就是我的工作:

  1. Run the server on the local host but set custom port not 80 (which is internet defualt port) 在本地主机上运行服务器,但将自定义端口设置为80(这是Internet defualt端口)
  2. make sure your Phone and laptop are connected to the same network 确保您的手机和笔记本电脑连接到同一网络
  3. Get your computer ip and in your phone go to the specific Wi-fi network configuration (long click on the network) and click the "Modify network config" -> click the "show advanced options" -> and set proxy setting to manual. 获取计算机IP,然后在手机中转到特定的Wi-fi网络配置(长按网络),然后单击“修改网络配置”->单击“显示高级选项”->并将代理设置设置为手动。 then in the host and port insert the computer ip and in the port the port in which the server is running. 然后在主机和端口中插入计算机ip,并在该端口中运行服务器的端口。
  4. In your app web client set the ip to to computer ip and port and you should be able to connect with your server when running the app 在您的应用程序Web客户端中,将ip设置为计算机ip和端口,并且在运行该应用程序时您应该能够与服务器连接

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

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