Connect app to backend on server via wifi with no internet connection

276 views Asked by At

Here is my problem, I need to have a webserver on a wifi with no internet connection. Now I have developed a web app so i can access it through my android and/or ios devices.

The issue happens with devices that have mobile data available, it seems to look for the local ip address of the webserver on the internet.

Is there a way to tell it to look on the local network? I hope my question makes sense.

1

There are 1 answers

0
Daniel Nugent On

On api-21 and up, you can use the openConnection() method of the Network class, and open a connection that is specific to the WiFi network

From the documentation:

Opens the specified URL on this Network, such that all traffic will be sent on this Network.

Here's how to use it:

HttpURLConnection con = null;
try {
    URL myurl = new URL("192.168.xx.x");
    con = (HttpURLConnection) NetworkingUtil.getWifiNetwork().openConnection(myurl);

    InputStream ins = con.getInputStream();

    //..............

} catch(Exception e) {
    e.printStackTrace();
}