java - Why Android heaps over a Thread task? - Stack Overflow

I am trying to check if my server is onoff from an Android application. From my MainActivity I use thi

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745296044a4621147.html

相关推荐

  • java - Why Android heaps over a Thread task? - Stack Overflow

    I am trying to check if my server is onoff from an Android application. From my MainActivity I use thi

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信