Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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

like image 425
Alien Avatar asked Dec 17 '16 06:12

Alien


People also ask

How to call localhost API from Android emulator?

If you're using Android Studio to run the emulator, then localhost of your host computer will be mapped to the IP address, 10.0. 2.2 , inside the emulator.

Does Android use REST API?

Retrofit is a REST Client library (Helper Library) used in Android and Java to create an HTTP request and also to process the HTTP response from a REST API. It was created by Square, you can also use retrofit to receive data structures other than JSON, for example SimpleXML and Jackson.

What is REST API in Android?

A RESTful API is an architectural style for an application program interface (API) that uses HTTP requests to access and use data. That data can be used to GET, PUT, POST and DELETE data types, which refers to the reading, updating, creating and deleting of operations concerning resources.

How to run Android app on localhost?

Start your server at localhost and attach the debugger. Next, change the API endpoints in your Android code to http://10.0.2.2 . This reroutes the requests from your emulator to your computer's localhost. Run the Android app on the emulator and cause the requests you want to debug.


2 Answers

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.

like image 94
rakesh kashyap Avatar answered Oct 08 '22 18:10

rakesh kashyap


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

like image 33
Mrinmoy Avatar answered Oct 08 '22 18:10

Mrinmoy