Timeout Error when connecting localhost api with Android Studio on Real devices through USB

我只是一个虾纸丫 提交于 2019-11-30 23:45:59

问题


I am using Android Studio version 3 on Window 8.1. There is XAMPP installed to host php api on localhost. I am trying to access the localhost api from Android Studio.

In the host name, I used IPv4 Address.

When I access it from Android Version 3, using Emulator. There is no problem at all. I am using Volley for api calls.

When I try to access using Real device through USB...I always get timeout error.

I am using Real device because my RAM is very low.

Please let me know if you need more info.

Update 1

I can confirm that Android 2.2.3 is working fine to run the apk on read device through USB


回答1:


I disabled the Firewall and everything is working perfectly.




回答2:


Your access to local host via IPV4 works on emulator because your emulator and the host are on same network. It gives timeout on real device because, real device is not connected to same network as your host by just connecting through USB.

There are two approaches:

  1. Ensure your device and host share the same network. It could be by connecting your device to host network through Wifi.

  2. If you cannot connect your device to the same network due to some constraints, then you can run following command:

    adb reverse tcp:<localhost-port> tcp:<localhost-port>

Then use localhost instead of IPV4 in your URL




回答3:


Your Network firewall Blocks the connection to localhost. To resolve this you can do :

In windows

1st Method Add Inbound rule (Recommended)

  • Create a new inbound rule with Windows Firewall with Advanced Security. The type in this case would be port, then on the next page you enter 8080 as the port. On the next page select either "Allow the connection", or "Allow the connection if it is secure". Select when the rule applies, and finally give the rule a name.

  • Connect the localhost api through the new allowed port.

  • Use ip-address of the localhost instead of localhost.

2nd Method Turn of firewall.

In Linux

1. Add IPTable Entry

  • You can learn adding incoming and outgoing rules using ssh and http in Linux here


来源:https://stackoverflow.com/questions/49782746/timeout-error-when-connecting-localhost-api-with-android-studio-on-real-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!