English 中文(简体)
将Android设备连接到本地主机上的Web服务
原标题:Connect an Android Device To a Web Service on Local Host

我为Android应用程序实现了一个web服务。web服务正在我的本地主机(192.168.1.2)上运行。使用Android模拟器,我成功连接到web服务。我尝试使用调试模式将我的Android设备连接到web服务,但没有成功。所以我的问题是,是否可以在不使用真实IP的情况下将Android设备连接到运行在我的本地主机(192.168.1.2)上的web服务?

问题回答

It s much simpler way supported by google!

  • Connect your phone via usb to computer and enable usb debugging
  • On your computer open Chrome browser and type exactly this address: chrome://inspect/#devices enter image description here
  • Now you can link your computer port to your device port by port forwarding button. On my computer I have service on address localhost:61437 and I just linked it to device s 8081 port. Remeber to check Enable port forwarding checkbox

  • screen from service on my computer ( localhost:61437 ) enter image description here

  • screen from my mobile browser with the same service ( localhost:8081). And that s it. Also you use this service address in your application enter image description here

你已经解决了你的问题吗?我也有和你一样的问题。以下是我已经完成的步骤:

  1. unplug lan cable or turn off any other internet connection from your pc.
  2. connect your android mobile to your pc using usb.
  3. turn on usb tethering
  4. back to your pc. check your ip. mine is 192.168.42.37
  5. check your webservice app in your pc. let s say http://192.168.42.37/webserviceapp
  6. back to your android mobile. try this url http://192.168.42.37/webserviceapp

现在,您可以通过手机访问电脑中的网络服务应用程序。

您的本地主机是127.0.0.1(或::1),LAN IP是192.168.1.2。在您的局域网下连接的每台电脑/设备都可以通过IP 192.168.1.2访问您的网络服务

你的Android设备必须在同一个局域网下连接,可能通过Wifi连接,这样它才能与192.168.1.2通话。

如果你不能在同一个局域网下连接你的Android设备,例如你只有一个3g连接,你需要使用路由器/防火墙将所有传入流量(可能只是http流量)从你的公共ip重定向到你的私人ip(192.168.1.2)

希望能帮上忙

我会加入我的过程,因为SO上没有任何东西对我有效。以下是我将物理android设备连接到本地主机上笔记本电脑(连接到手机)上运行的web服务的步骤:

  1. 在Android设备上启用USB调试

  2. 在您的计算机上运行web服务。我的web服务运行在localhost上,端口3000正在开发中:<code>http://localhost:3000/api/...

  3. 运行ifconfig(Unix),或ipconfig(Windows)

  4. Find your machine s inet address on your LAN interface. Mine is 10.0.0.121 for interface wlan0. Externally, it is 68.43.XX.XXX, which is not the address that you want to use.
  5. Use the LAN IP since you are connecting to your service on LAN, otherwise you might get an econnrefused (connection refused) error due to firewall rules
  6. Build your http URL with that IP address, and the port that your web service is running on. For me, it s http://10.0.0.121:3000/api/...

当你启动你的应用程序时,你应该在日志、Wireshark等中连接到你的本地web服务,并且你应该在你的Android应用程序中看到所需的活动/数据。

我也遇到过同样的问题,研究了很多,然后发现你必须明确地更改防火墙设置。您的防火墙正在阻止从外部源访问您的代码。所以,您所需要做的就是,转到防火墙设置,为入站和出站添加端口80(在我的情况下,因为我使用的是Apachehttp服务器)。现在,您可以在手机浏览器上进行测试http://192.16.**:80/

我在Mac上使用了GasMask和Charles Proxy Server。你的手机和电脑必须在同一个网络上。

假设您要访问的Web服务url位于http://api.xyz.com,首先使用GasMask将该url指向本地主机,然后使用Charles设置代理服务器。然后你进入手机上的设置,进入Wi-Fi,长按你连接的网络,选择“修改网络”,然后输入Charles给你的代理设置。

在我的情况下,这些解决方案都不起作用,因为Windows防火墙会阻止它,但在防火墙上设置规则并没有效果。

我的问题是,我的笔记本电脑连接了Wifi,而Windows的Wifi连接就像公共网络一样。我必须将网络连接更改为专用网络http://www.comofuncionatodo.net/tecnologia/informatica/como-cambiar-de-red-publica-a-red-privada-en-windows-10/

如果你不想在互联网上公开你的DEV网络服务,我同意其他答案。然而,如果您只公开Web服务,则会容易得多。有很多免费的DNS服务,但我发现没有一个ip是最容易设置的。我使用它的目的正是你所问的;这样我就可以在真实的设备上使用我的DEV Web服务进行测试。

如果你选择不带ip(我与那家公司没有任何关系,它只是我使用过并熟悉的一家),你可以获得一个免费的公共访问URL,如http://MyExampleWebServer.no-ip-org,并且没有一个ip有可以安装的实用程序,所以即使你在动态ip后面,它也会始终保持与该URL相关联的正确外部ip。如果你在家工作,那么你只需要确保将流量从端口80端口转发到你的内部192.x.x.x IP地址(或者你使用的任何端口;对于ssl,可能是443)。

就这么简单,现在你可以从任何可以访问互联网的设备上访问该网络服务。

我没有使用过它,但我相信dyndns也提供类似的服务。

此解决方案适用于Eclipse中的GAE开发服务器

步骤1:获取LAN IP

转到Windows命令控制台(按Win+R,然后键入“cmd”)。在控制台中,输入“ipconfig”。您将看到一个显示列表。在无线局域网适配器Wi-Fi下,获取IPv4地址。这将是192.168.x.x

局域网IP:192.168.x.x

第2步:

转到Eclipse,打开配置的服务器

在GAE开发服务器的属性下->;要绑定到的本地接口地址,输入LAN IP地址,然后保存。

步骤3:

现在您可以通过以下方式访问GAE服务器

http://192.168.x.x:8888/

8888-指GAE开发服务器中提到的端口号

要使用本地web服务自己的服务器主机而不是带有端口的IP地址访问本地web服务,请执行以下步骤:

  1. Make sure your Android device and your local machine are on the same network.
  2. Install SquidMan on your Mac, Linux, or any other Proxy Server.
  3. Configure the proxy server s HTTPPort (ex. 5555) and clients (ex. 192.168.0.0/24) to your own network mask, and run the proxy server.
  4. 您正在使用中的web服务:

    a.网络浏览器:通过修改WiFi网络配置Android设备的代理设置。

    b. Android application: Set up the Proxy for your HTTP client. If you are using Volley, check this out: Volley Behind a Proxy server.

  5. You can now connect to it by using whatever URL you are using on your host to connect to the web service (ex. http://my-local-machine.com)

提示:如果您有4xx响应代码,请确保您的web服务允许来自其他非本地主机的连接。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签