检查服务器是否可用?

时间:2015-03-04 13:47:46

标签: android networking connectivity

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

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;
}

2 个答案:

答案 0 :(得分:1)

看看这段代码:

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();
            }                  
      }           
   }
}

答案 1 :(得分:0)

您只能使用AsyncTask(http://developer.android.com/guide/components/processes-and-threads.html

它是在后台运行短异步任务的android方式,并且在完成后它有一个回调方法