Android的网络Http判断是否连接服务器

<span style="font-size:24px;">public boolean isConnByHttp(){
   boolean isConn = false;
   URL url;
    HttpURLConnection conn = null;
     try {
       url = new URL("http://www.baidu.com");//你的服务器IP
       conn = (HttpURLConnection)url.openConnection();
       conn.setConnectTimeout(1000*5);
     if(conn.getResponseCode()==200){
        isConn = true;
         } 
         } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
          e.printStackTrace();
        }catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }finally{
          conn.disconnect();
         }
       return isConn;
 }</span>

你可能感兴趣的:(网络,服务器)