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 stati
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
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.
Check your ip:- Steps to check ip (make sure you are connected to internet)
Now make your url like this: http://192.168.240.2/index.html
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, OPTIONS
allowed-headers: "*"
exposed-headers:
allow-credentials: true
max-age: 1800
Thank you all for your help, it is much appreciated.