13

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 String url) {
        StringBuilder result = new StringBuilder();
        HttpURLConnection urlConnection = null;
        try {
            String apiUrl = getAbsoluteUrl(url); // concatenate uri with base url eg: localhost:8080/ + uri
            URL requestUrl = new URL(apiUrl);
            urlConnection = (HttpURLConnection) requestUrl.openConnection();
            urlConnection.connect(); // no connection is made
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }
        return result.toString();
    }

I can access my api via my device's browser. However, when I use this same url within the built apk to make the request, no connection is made.

My manifest includes:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Side notes:

I am connecting my device to my laptop running the rest api via usb. I am using the WLAN IPv4 address found by calling ipconfig.

Any tips in the right direction would be much appreciated - thanks!

Edit to include chrome browser (on android device) output from local REST api running on my laptop (A GET request to return default guest user information): enter image description here

Alien
  • 444
  • 2
  • 9
  • 28
  • You can access `localhost:8080` from the **android browser**? Probably not. `localhost` means different things on all devices. – OneCricketeer Dec 17 '16 at 06:45
  • 1
    try connecting to your device via localhost check [this](http://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device), access your file and parse it – Iamat8 Dec 17 '16 at 06:45
  • @cricket_007: I can access my WLAN IPv4 address + URI from my android browser. I have an api as follows: – Alien Dec 17 '16 at 14:06
  • /api/users which returns all users with a get request. I can see the output JSON in my android chrome browser. But when I make this call in my android device apk build, I do not get a connection. – Alien Dec 17 '16 at 14:07
  • @Alien I Am facing same issue. Have you solution for this ? – GNK Aug 27 '20 at 08:54

4 Answers4

10

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.

rakesh kashyap
  • 1,418
  • 22
  • 41
7

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Mrinmoy
  • 1,370
  • 2
  • 18
  • 28
  • Hey great thanks for the image upload, however I have tried this as well and still no luck from my apk file. I have attached an image in my original question to show the output from my device browser. I am calling this same address from my android build and not retrieving any response. – Alien Dec 17 '16 at 14:33
5

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

Shadow Droid
  • 1,696
  • 1
  • 12
  • 26
  • 2
    I can access my rest url no problem from my tablet device browser, but when I access the same url from my apk build I do not get a connection. – Alien Dec 17 '16 at 16:29
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, OPTIONS
        allowed-headers: "*"
        exposed-headers:
        allow-credentials: true
        max-age: 1800

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

Alien
  • 444
  • 2
  • 9
  • 28