Android中的互联网连接

时间:2014-09-11 15:01:36

标签: android http ping

是否有比下面更好的选项,以检查互联网上的连接。我知道Wifi或移动数据已连接,但我想知道我的应用是否能够连接到互联网接收数据:

class checkInternetNetwork extends AsyncTask<String, String, String> 
{
    @Override
    protected String doInBackground(String... params) 
    {
        internetavailable = isInternetConnected();

        return null;
    }

    protected void onPostExecute(String file_url) 
    {

        if (internetavailable == false)
        {
            Log.e("Check","internetavailable");

            Toast.makeText(getActivity().getApplicationContext(), "Unable to connect to server. Check your network!!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
    }
}

我有这样的isInternetConnected方法:

  public boolean isInternetConnected() 
{
    final int CONNECTION_TIMEOUT = 1000;
    try 
    {
        HttpURLConnection mURLConnection = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
        mURLConnection.setRequestProperty("User-Agent", "ConnectionTest");
        mURLConnection.setRequestProperty("Connection", "close");
        mURLConnection.setConnectTimeout(CONNECTION_TIMEOUT);
        mURLConnection.setReadTimeout(CONNECTION_TIMEOUT);
        mURLConnection.connect();
        return (mURLConnection.getResponseCode() == 200);
    } 
    catch (IOException ioe) 
    {

    }
    return false;

}

问题是internetAvailable总是显示为false。

对此的解决方案非常有用。

谢谢!

3 个答案:

答案 0 :(得分:1)

这绝对是一种简单的方法。更好的方法是实际检查您尝试使用的Web服务。因为假设Google.com可能会关闭,而您的网络服务仍在运行,反之亦然,Google.com可能会正常运行,您的网络服务可能会停止运行。

您还可以尝试:(See Documentation:使用&#34; ConnectivityManager查询活动网络并确定其是否具有Internet连接。&#34;)

public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

答案 1 :(得分:0)

你应该在ConnectivityManager中查询这样的内容:

ConnectivityManager cm = context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo;
boolean isConnected = ni != null && ni.isConnected()

请务必查看ConnectivityManager和NetworkInfo的文档,因为您可以获得更多数据:

答案 2 :(得分:0)

试试这个兄弟,创建一个名为CheckNetwork的类。将我的检查员代码放在课堂内。

private static final String TAG = CheckNetwork.class.getSimpleName();

public static boolean isInternetAvailable(Context context) {
    NetworkInfo info = (NetworkInfo) ((ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();

    if (info == null) {
        Log.d(TAG, "no internet connection");
        return false;
    } else {
        if (info.isConnected()) {
            Log.d(TAG, " internet connection available...");
            return true;
        } else {
            Log.d(TAG, " internet connection");
            return true;
        }

    }
}

检查班级中的网络连接

  

if(CheckNetwork.isInternetAvailable(YourClass.this)){

     

//如果有互联网

     

}

     

其他{

     

//如果没有互联网连接

     

}

进行必要的进口。如果您尝试访问错误的URL,请确保检查&#34; json&#34;或者您获取的数据传输具有值,否则它将返回nullpointerexception。希望这可以帮助。欢呼声。