如何在 Android 设备上浏览本地主机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3378501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to browse localhost on Android device?
提问by Upvote
I try to browse localhost on my HTC Magic. I have connected my device with Eclipse via USB. When browsing http://10.0.2.2I get "Page not available". I remember, some days ago it worked.
我尝试在我的 HTC Magic 上浏览本地主机。我已通过 USB 将我的设备与 Eclipse 连接。浏览http://10.0.2.2 时出现“页面不可用”。我记得,几天前它起作用了。
But on the emulator I am able to browse localhost
但是在模拟器上我可以浏览本地主机
Any ideas?
有任何想法吗?
采纳答案by Fedor
I use my local ip for that i.e. 192.168.0.1 and it works.
我使用我的本地 IP,即 192.168.0.1,它可以工作。
回答by tosha Shah
Easier way to check is in browser of emulator type 10.0.2.2 instead of localhost.
更简单的检查方法是在模拟器类型 10.0.2.2 而不是 localhost 的浏览器中。
Hope that helps! :)
希望有帮助!:)
回答by Fedor
to access localhost on emulator: 10.0.2.2. However this may not always work for example if you have something other than a web server such as XMPP server.
在模拟器上访问本地主机:10.0.2.2。但是,这可能并不总是有效,例如,如果您拥有除 Web 服务器之外的其他东西,例如 XMPP 服务器。
assuming that you're on the same wireless network...
假设你在同一个无线网络上......
find your local ip (should be something 192.168.1.x) - go to the command line and type 'ipconfig' to get this. where x is the assigned local ip of the server machine.
use your local ip for your android device to connect to your localhost.
找到您的本地 ip(应该是 192.168.1.x) - 转到命令行并输入“ipconfig”以获取此信息。其中 x 是分配给服务器机器的本地 ip。
使用您的 android 设备的本地 ip 连接到您的本地主机。
it worked for me.
它对我有用。
回答by Michael Denham
If you want to access a server running on your PC from your Android device via your wireless network, first run the command ipconfig on your PC (use run (Windows logo + R), cmd, ipconfig).
如果要通过无线网络从 Android 设备访问 PC 上运行的服务器,请首先在 PC 上运行命令 ipconfig(使用 run (Windows logo + R)、cmd、ipconfig)。
Note the IPv4 address: (it should be 192.168.0.x) for some x. Use this as the server IP address, together with the port number, e.g. 192.168.0.7:8080, in your code.
请注意某些 x 的 IPv4 地址:(它应该是 192.168.0.x)。在您的代码中使用它作为服务器 IP 地址以及端口号,例如 192.168.0.7:8080。
Your Android device will then access the server via your wireless network router.
然后,您的 Android 设备将通过您的无线网络路由器访问服务器。
回答by Dimitris
I needed to see localhost on my android device as well (Samsung S3) as I was developing a Java Web-application.
By far the fastest and easiest way is to go to this link and follow instructions: https://developer.chrome.com/devtools/docs/remote-debugging
在开发 Java Web 应用程序时,我还需要在我的 android 设备(三星 S3)上看到 localhost。
到目前为止,最快和最简单的方法是转到此链接并按照说明操作:https: //developer.chrome.com/devtools/docs/remote-debugging
* Note: You have to use Google Chrome.*
My summary of the above link:
* 注意:您必须使用谷歌浏览器。*
我对上述链接的总结:
- Connect PC with Phone over USB.
- Turn on Phone's "Developer options" from Settings
- Go to about:inspect URL in PC's browser
- Check "Discover USB Devices" (forward Ports if you are using them in your web-application)
- Copy/paste localhost required link to text field in browser and click Open.
- 通过 USB 将 PC 与手机连接。
- 从设置中打开手机的“开发者选项”
- 前往 about:inspect URL in PC's browser
- 检查“发现 USB 设备”(如果您在 Web 应用程序中使用它们,则转发端口)
- 将 localhost 所需的链接复制/粘贴到浏览器中的文本字段,然后单击“打开”。
Piece of cake
小菜一碟
回答by Shobhit Puri
You can get a public URL for your server running on a specific port on localhost.
您可以获得运行在本地主机特定端口上的服务器的公共 URL。
At my work place I could access the local server by using the local IP address of my machine in the app, as most of the other answers suggest. But at home I wasn't able to do that for some reason. After trying many answers and spending many hours, I came across https://ngrok.com. It is pretty straight forward. Just download it from within the folder do:
在我的工作场所,我可以通过在应用程序中使用我机器的本地 IP 地址来访问本地服务器,正如大多数其他答案所建议的那样。但是在家里,由于某种原因,我无法做到这一点。在尝试了很多答案并花了很多时间之后,我遇到了https://ngrok.com。这是非常直接的。只需从文件夹中下载它:
ngrok portnumber
( from command prompt in windows)
(从 Windows 中的命令提示符)
./ngrok portnumber
(from terminal in linux)
(来自linux中的终端)
This will give you a public URL for your local server running on that port number on localhost. You can include in your app and debug it using that URL.
这将为您提供在 localhost 上的该端口号上运行的本地服务器的公共 URL。您可以包含在您的应用程序中并使用该 URL 对其进行调试。
You can securely expose a local web server to the internet and capture all traffic for detailed inspection. You can share the URL with your colleague developer also who might be working remotely and can debug the App/Server interaction.
您可以安全地将本地 Web 服务器公开到 Internet 并捕获所有流量以进行详细检查。您可以与您的同事开发人员共享 URL,他们也可能在远程工作并且可以调试应用程序/服务器交互。
Hope this saves someone's time someday.
希望有一天这可以节省某人的时间。
回答by Rob
Combining a few of the answers above plus one other additional item solved the problem for me.
结合上面的一些答案加上另外一个附加项目为我解决了这个问题。
- As mentioned above, turn your firewall off [add a specific rule allowing the incoming connections to the port once you've successfully connected]
- Find you IP address via ipconfig as mentioned above
- Verify that your webserver is binding to the ip address found above and not just 127.0.0.1. Test this locally by browsing to the ip address and port. E.g. 192.168.1.113:8888. If it's not, find a solution. E.g. https://groups.google.com/forum/?fromgroups=#!topic/google-appengine-java/z4rtqkKO2hg
- Now test it out on your Android device. Note that I also turned off my data connection and exclusively used a wifi connection on my Android.
- 如上所述,关闭您的防火墙[添加一个特定规则,在您成功连接后允许传入连接到端口]
- 如上所述通过 ipconfig 找到您的 IP 地址
- 验证您的网络服务器是否绑定到上面找到的 IP 地址,而不仅仅是 127.0.0.1。通过浏览到 IP 地址和端口在本地进行测试。例如 192.168.1.113:8888。如果不是,请找到解决方案。例如https://groups.google.com/forum/?fromgroups=#!topic/google-appengine-java/z4rtqkKO2hg
- 现在在您的 Android 设备上进行测试。请注意,我还关闭了数据连接,并在我的 Android 上专门使用了 wifi 连接。
回答by makt
If your localhost is not running on the default HTTP port(which is port 80), you need to specify the port in your url to something that corresponds to the port on which your localhost is running. E.g. If your localhost is running on, say port 85, Your url should be
如果您的 localhost 未在默认 HTTP 端口(即端口 80)上运行,您需要在您的 url 中将端口指定为与您的 localhost 正在运行的端口相对应的内容。例如,如果您的本地主机正在运行,请说端口 85,您的 url 应该是
http://10.0.2.2:85
回答by Misagh Aghakhani
If your firewall is on, turn it off and use IPv4 to test your app in the actual device, then test your application.
如果您的防火墙已打开,请将其关闭并使用 IPv4 在实际设备中测试您的应用程序,然后测试您的应用程序。
回答by Deepika Rajani
I had similar issue but I could not resolve it using static ip address or changing firewall settings. I found a useful utility which can be configured in a minute.
我有类似的问题,但我无法使用静态 IP 地址或更改防火墙设置来解决它。我发现了一个有用的实用程序,可以在一分钟内完成配置。
We can host our local web server on cloud for free. On exposing it on cloud we get a different URL which we can use instead of localhost and access the webserver from anywhere.
我们可以免费在云上托管我们的本地 Web 服务器。在云上公开它时,我们会得到一个不同的 URL,我们可以使用它而不是 localhost 并从任何地方访问网络服务器。
The utility is ngrok https://ngrok.com/downloadSteps:
该实用程序是 ngrok https://ngrok.com/download步骤:
- Signup
- Download
- Extract the file and double click to run it, this will open a command prompt
- Type "ngrok.exe http 80" without quotes to host for example XAMPP apache server which runs on port 80.
- Copy the new url name generated on the cmd prompt for e.g. if it is like this "fafb42f.ngrok.io"
- 注册
- 下载
- 解压文件并双击运行它,这将打开一个命令提示符
- 键入不带引号的“ngrok.exe http 80”以托管例如在端口 80 上运行的 XAMPP apache 服务器。
- 复制在 cmd 提示符下生成的新 url 名称,例如,如果它是这样的“fafb42f.ngrok.io”
URL like : http://localhost/php/test.phpShould be modified like this : http://fafb42f.ngrok.io/php/test.php
URL,如:HTTP://localhost/php/test.php应修改如下:http://fafb42f.ngrok.io/php/test.php
Now this URL can be accessed from phone.
现在可以从手机访问此 URL。