Skip to content Skip to sidebar Skip to footer

How Can I Access My Local Rest Api From My Android Device?

I have a spring REST api running locally on my computer. I would like to consume this api for android development. Here is my get request: public static String sendGet(final Stri

Solution 1:

Let me tell you an easier way to do this. If you are using Android emulator you can use 10.0.2.2 as the IP address to connect to the host machine where your REST API is available.

Similarly if you are using Genymotion which uses Oracle Virtualbox, you can use 10.0.3.2.

Solution 2:

Check your ip:- Steps to check ip (make sure you are connected to internet)

  1. Open command prompt
  2. type ipconfig
  3. Ip is the highlighted one in image below

enter image description here

Now make your url like this: http://192.168.240.2/index.html

Solution 3:

Your rest url must be something like this - http://localhost:8080/yourRest/restMethod .Instead of localhost url connect your mobile and local machine on same network(wifi network). Get the ip address of your local machine e.g 192.168.1.X ...so now your end point url for rest will be http://192.168.1.X:8080/yourRest/restMethod

Solution 4:

SOLVED if anyone is interested:

I managed to fix this issue by extending the class my original sendGet(final String url) was in as follows HttpClientUsage extends AsyncTask<String, Void, String> more information and a tutorial can be found here: AsyncTask tutorial

I then had to configure my CORS settings on my local REST API as follows:

cors:allowed-origins:"*"allowed-methods:GET,PUT,POST,DELETE,OPTIONSallowed-headers:"*"exposed-headers:allow-credentials:truemax-age:1800

Thank you all for your help, it is much appreciated.

Post a Comment for "How Can I Access My Local Rest Api From My Android Device?"