【问题标题】:checking internet connection on android programming检查android编程的互联网连接
【发布时间】:2015-03-31 05:58:33
【问题描述】:

我尝试编写一个安卓程序来使用两种不同的方法检查互联网连接。第一种是最常用的方法,CheckInternetConnection(),第二种方法是通过连接到一个网站,ConnectGoogleTest()。 第一个工作完美,但在第二个中我的平板电脑挂了!有人知道为什么吗?

代码是:

public class myITClass {
private Context ctx ;
public myITClass(Context context){
    this.ctx = context;
}

public boolean CheckInternetConnection() {
    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    //NetworkInfo ni = cm.getActiveNetworkInfo();
    if (cm.getActiveNetworkInfo() == null) {
        // There are no active networks.
        return false;
    } else {

        return true;
    }
  }
public boolean googlePingTest(){
    boolean res = false ;
    try {
        URL url = new URL("http://www.google.com/");
        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        urlc.setConnectTimeout(15000);
        urlc.connect();
        if (urlc.getResponseCode() == 200) { res = true; }
    } catch (MalformedURLException e1) {
        res = false;
    } catch (IOException e) {
        res = false ;
    }catch (Exception e){
        res = false ;
    }

    return res;
}
}

【问题讨论】:

  • 我不认为你可以设置连接超时之后调用openConnection()。 logcat 中的任何内容?
  • 我在这里回答了同样的问题,并且工作正常。唯一不同的是为连接设置了几个属性。 stackoverflow.com/questions/27988103/…

标签: android connection httpconnection


【解决方案1】:

第二种方法在主线程上同步调用网络,这会阻塞 UI。尝试使用 AsyncTask。

【讨论】:

    【解决方案2】:

    您可以通过HttpURLConnectionhttp://www.google.com/ 发送ping。请确保您在后台线程中执行此操作。创建网络任务必须在后台运行。有 3 个选项可以做到这一点:

    这一次,我们将使用AsyncTask。所以在你的Activity 中创建一个私有类:

    private boolean res = false;
    private class PingTask extends AsyncTask<String, String, String> {
    
        @Override
        protected String doInBackground(String... urlSite) {
            HttpURLConnection urlc = null;
    
            try {
              URL url = new URL("http://www.google.com/");
              urlc = (HttpURLConnection) url.openConnection();
              urlc.setConnectTimeout(15000);
              urlc.setRequestMethod("GET");
              urlc.connect();
    
             if (urlc.getResponseCode() == 200) { res = true; }
          } catch (MalformedURLException e1) {
             res = false;
          } catch (IOException e) {
             res = false ;
          }catch (Exception e){
             res = false ;
          }finally{
                if (urlc != null) {
                    try{
                        // close the connection
                        urlc.disconnect();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
            return null;
         }
    }
    

    别忘了在你的类中添加这个字段:

    private boolean res = false;
    

    创建一个获取状态的方法:

    public boolean getStatus(){
        return status;
    }
    

    如何使用?

    先执行PingTask查看状态:

    PingTask ping = new PingTask();
    ping.execute();
    

    获取状态:

    // if the connection is available
    if(getStatus()==true){
        // do your thing here.
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-12
      • 1970-01-01
      • 2016-08-28
      • 2012-02-21
      • 2016-01-16
      • 2017-11-30
      • 2013-02-02
      • 2011-02-14
      • 2012-03-23
      相关资源
      最近更新 更多