Reputation: 2538
Note: I know that similar questions have been asked here several times, but none of the suggested solutions fixed the problem for me (or I might have missed the correct solution).
I have an Android app which is supposed to talk to a REST API, which is also running on my localhost. However, I cannot access the API, neither from device nor from emulator. These are the URLs I have tried:
Interestingly, I can access the API from device's browser, and also from emulator's browser.
Note that my device is connected to the same WiFi as my laptop, and I have internet permission in my app:
<uses-permission android:name="android.permission.INTERNET"/>
I thought this problem might be happening due to the REST library I use (Volley), however I still cannot access the API by using standard classes from java.net. I am getting 404 from 192.168.0.xx, and timeout from 192.168.1.xx and 10.0.2.2.
I have tried other things like turning on USB tethering, using a different port for my API, disabling WiFi and trying over mobile data etc. but none of them fixed my problem.
Any idea what's going wrong here?
Upvotes: 0
Views: 1373
Reputation: 1006539
Using 10.0.2.2 should work, for an emulator accessing a Web app on the development machine that is running the emulator.
For everything else — emulator accessing another machine, or hardware accessing anything — use a routable IP address that reaches that machine.
Your 192.168.0.xx value would appear to be the correct one, as you are getting a 404 error. The only way you will get a 404 is if the server sends that error code to you. If you cannot connect to the server, you will get some sort of exception, not a 404.
Debugging why you are getting a 404 response, rather than a 200 and the desired Web service output, is left as an exercise for the reader. :-)
Upvotes: 1