【问题标题】:check if the server is available?检查服务器是否可用?
【发布时间】:2015-03-04 13:47:46
【问题描述】:

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

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

【问题讨论】:

    标签: android networking connectivity


    【解决方案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();
                }                  
          }           
       }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 但是如何把它放在doInBackground中!!
      猜你喜欢
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多