检查服务器是否可用? [英] check if the server is available?

查看:73
本文介绍了检查服务器是否可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个连接到服务器的应用程序.因此,我想使用波纹管函数来检查服务器是否可用,但我不知道如何在 线程中使用它,以及每次需要检查服务器是否在活动中可用时如何调用它或片段:

I developing an application which connects to the server. Therefore i want use the bellow function to check if the server is available , But I don't know how I can use it in a thread and how call it each time when I need check if the server is available in activity or fragment :

static public boolean isURLReachable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        try {
            URL url = new URL("http://192.168.1.13");   // Change to "http://google.com" for www  test.
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(10 * 1000);          // 10 s.
            urlc.connect();
            if (urlc.getResponseCode() == 200) {        // 200 = "OK" code (http connection is fine).
                Log.wtf("Connection", "Success !");
                return true;
            } else {
                return false;
            }
        } catch (MalformedURLException e1) {
            return false;
        } catch (IOException e) {
            return false;
        }
    }
    return false;
}

推荐答案

看看这段代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    ConnectivityManager cm = (ConnectivityManager)  getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        MyTask task = new MyTask();
        task.execute();
    }
}


private class MyTask extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Boolean doInBackground(Void... params) {

         try {

        URL url = new URL("http://192.168.1.13");   // Change to "http://google.com" for www  test.
        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        urlc.setConnectTimeout(10 * 1000);          // 10 s.
        urlc.connect();
        if (urlc.getResponseCode() == 200) {        // 200 = "OK" code (http connection is fine).
            Log.wtf("Connection", "Success !");
            return true;
        } else {
            return false;
        }
    } catch (MalformedURLException e1) {
        return false;
    } catch (IOException e) {
        return false;
    }
 }

    @Override
    protected void onPostExecute(Boolean result) {
        boolean bResponse = result;
         if (bResponse==true)
            {
                Toast.makeText(MainActivity.this, "server is available", Toast.LENGTH_SHORT).show();      
            }
            else
            {           
                Toast.makeText(MainActivity.this, "server is not available", Toast.LENGTH_SHORT).show();
            }                  
      }           
   }
}

这篇关于检查服务器是否可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆