0

I have created an endpoint of an API using the FastAPI module of Python. Now I'm accessing the API endpoint using the URL "http://127.0.0.1:8000/predict?age=12&sex=M". After this, I used the IP Address 10.0.2.2 (URL: "http://10.0.2.2/predict?age=12&sex=M") to access this API from the Android emulator inside the Android studio. In both scenarios, the API is working fine and giving a valid response. Now I'm trying to access this API endpoint from my physical Android device with my laptop's IPv4 Address (URL : "http://<My IPv4 address>/predict?age=12&sex=M") and getting null in response. So please do let me know any suggestions and solutions to solve this issue.

My laptop and Mobile phone are both connected to the same wifi network.

Ashutosh
  • 31
  • 5
  • Android versions of emulator and device please. – blackapps Apr 26 '23 at 11:15
  • the IP 10.0.2.2 what is? why you have used it? – Paolino L Angeletti Apr 26 '23 at 12:49
  • @blackapps the emulator is having Android 12 and the physical device is having Android 10. – Ashutosh Apr 26 '23 at 13:01
  • @PaolinoLAngeletti, the IP 10.0.2.2 is a special alias for your host in other words for 127.0.0.1. For more details, you can refer : 1. https://developer.android.com/studio/run/emulator-networking 2. https://stackoverflow.com/questions/9808560/why-do-we-use-10-0-2-2-to-connect-to-local-web-server-instead-of-using-computer – Ashutosh Apr 26 '23 at 13:02
  • Is the API listening to 127.0.0.7 and 10.0.0.2 only or listening to the external IP address only? Does the API change to 10.0.0.2 for the external calls also? Where does the Android device think 10.0.0.2 lives? Probably it uses the local subnet as directly connected and sends the 10.0.0.2 to the default gateway. Can you change the 10.0.0.2 in the API to the external IP of your API system? – Ljm Dullaart Apr 28 '23 at 08:21
  • I am getting same issue. Did u solve it? if yes then how?? – Vineet Pandey Jul 16 '23 at 08:47

0 Answers0