Java 检查internet连接超时

Java 检查internet连接超时,java,android,connection,ping,Java,Android,Connection,Ping,我正在通过ping谷歌检查互联网连接状态。问题是,当没有连接且等待时间过长时 这是我的代码: private boolean checkInternet() { String netAddress = null; try { netAddress = new NetTask().execute("www.google.com").get(); return (!netAddress.equals("")); } catch

我正在通过ping谷歌检查互联网连接状态。问题是,当没有连接且等待时间过长时

这是我的代码:

private boolean checkInternet() {
    String netAddress = null;
    try
    {
        netAddress = new NetTask().execute("www.google.com").get();
        return (!netAddress.equals(""));
    }
    catch (Exception e1)
    {
        e1.printStackTrace();
        return false;
    }
    return false;
}

public class NetTask extends AsyncTask<String, Integer, String>
{
    @Override
    protected String doInBackground(String... params)
    {
        InetAddress addr = null;
        try
        {
                addr = InetAddress.getByName(params[0]);
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
            return "";
        } catch (IOException time)
        {
            time.printStackTrace();
            return "";
        }
        return addr.getHostAddress();
    }
}
private boolean checkInternet(){
字符串netAddress=null;
尝试
{
netAddress=new NetTask().execute(“www.google.com”).get();
返回(!netAddress.equals(“”);
}
捕获(异常e1)
{
e1.printStackTrace();
返回false;
}
返回false;
}
公共类NetTask扩展异步任务
{
@凌驾
受保护的字符串doInBackground(字符串…参数)
{
InetAddress addr=null;
尝试
{
addr=InetAddress.getByName(参数[0]);
}
捕获(未知后异常e)
{
e、 printStackTrace();
返回“”;
}捕获(异常时间)
{
time.printStackTrace();
返回“”;
}
返回地址getHostAddress();
}
}

我无法连接
isReachable(int timeout)
,因为它返回一个
布尔值。如何解决这个问题?

如果一个方法不能在分配的时间内完成,有几种方法可以取消该方法

第一个答案可能是我会走的路。在这里,它被插入到您的示例中

ExecutorService executor = Executors.newCachedThreadPool();
Callable<Object> task = new Callable<Object>() {
    public Object call() {
        String netAddress = new NetTask().execute("www.google.com").get();
        return (!netAddress.equals(""));
    }
};
Future<Object> future = executor.submit(task);
try{
    //Give the task 5 seconds to complete
    //if not it raises a timeout exception
    Object result = future.get(5, TimeUnit.SECONDS);
    //finished in time
    return result; 
}catch (TimeoutException ex){
    //Didn't finish in time
    return false;
}
ExecutorService executor=Executors.newCachedThreadPool();
可调用任务=新的可调用任务(){
公共对象调用(){
字符串netAddress=new NetTask().execute(“www.google.com”).get();
返回(!netAddress.equals(“”);
}
};
未来=执行者提交(任务);
试一试{
//给任务5秒钟的时间完成
//如果不是,则引发超时异常
对象结果=future.get(5,TimeUnit.SECONDS);
//按时完成
返回结果;
}捕获(TimeoutException例外){
//没有及时完成
返回false;
}

如果一个方法没有在分配的时间内完成,有几种方法可以取消它

第一个答案可能是我会走的路。在这里,它被插入到您的示例中

ExecutorService executor = Executors.newCachedThreadPool();
Callable<Object> task = new Callable<Object>() {
    public Object call() {
        String netAddress = new NetTask().execute("www.google.com").get();
        return (!netAddress.equals(""));
    }
};
Future<Object> future = executor.submit(task);
try{
    //Give the task 5 seconds to complete
    //if not it raises a timeout exception
    Object result = future.get(5, TimeUnit.SECONDS);
    //finished in time
    return result; 
}catch (TimeoutException ex){
    //Didn't finish in time
    return false;
}
ExecutorService executor=Executors.newCachedThreadPool();
可调用任务=新的可调用任务(){
公共对象调用(){
字符串netAddress=new NetTask().execute(“www.google.com”).get();
返回(!netAddress.equals(“”);
}
};
未来=执行者提交(任务);
试一试{
//给任务5秒钟的时间完成
//如果不是,则引发超时异常
对象结果=future.get(5,TimeUnit.SECONDS);
//按时完成
返回结果;
}捕获(TimeoutException例外){
//没有及时完成
返回false;
}

添加相应的异常后,您的代码可以完美地工作。有必要强制转换布尔变量结果。非常感谢你!通过添加相应的异常,您的代码可以完美地工作。有必要强制转换布尔变量结果。非常感谢你!