How to check if the response of a server returns (200 OK) in java? [closed]

0

I'm developing an android app, and would like to check if a website or a URL is active

    
asked by anonymous 10.12.2015 / 19:29

1 answer

1

The verification can be done using Android's own REST APIs, making a GET call from a URL and waiting for a Response, from that Response you make the logic on top of the Status Code you receive.

link link

One of the Android REST APIs - link

Another alternative is to use the Java API itself to check, but it will be limited to the package API java.net

Ex:

public int getStatusCode(String urlPath) {
    try {
        URL url = new URL(urlPath); // http://www.seusite.com.br
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.connect();
        return conn.getResponseCode();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
    
10.12.2015 / 19:52