carl
carl

Reputation: 396

Why Android heaps over a Thread task?

I am trying to check if my server is on/off from an Android application. From my MainActivity I use this code snippet:

try {
    new CheckServer().start();
} catch (MalformedURLException e) {
     throw new RuntimeException(e);
}

This is the class:

public class CheckServer extends Thread {

    URL url = new URL("server-address");

    public CheckServer() throws MalformedURLException {
    }

    @Override
    public void run() {
        try
        {
            HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
            int code = urlConnection.getResponseCode();
                    
            Log.d("test", "test connection > " + code);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

When I run the app, nothing happens(I don't get the message Log). So after a debugging I discovered that in runtime this part was skipped:

public void run() {
        try
        {
            HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
            int code = urlConnection.getResponseCode();
            Log.d("test", "test connection > " + code);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

Can anyone show/tell me why or what I am doing wrong? it will be highly appreciate

Upvotes: 0

Views: 33

Answers (0)

Related Questions