1
Googleにpingを実行してインターネット接続状態を確認しています。問題は、接続がなく、待ち時間が超過拡張されている場合です。インターネット接続をチェックするためのタイムアウトAndroid
これは私のコードです:
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();
}
}
それはboolean
を返すので、私はisReachable(int timeout)
を連結することはできません。それをどうすれば解決できますか?
あなたのコードは、対応する例外を完全に追加して動作します。ブール変数の結果をキャストする必要がありました。どうもありがとうございました ! – Mike