android 入门学习笔记 检查网络连接是否正常 判断是否为横屏

import java.net.HttpURLConnection; 
/* 检查网络连接是否正常 */ 
  public boolean checkInternetConnection 
  (String strURL, String strEncoding) 
  { 
    /* 最多延时n秒若无应答则表示无法连接 */ 
    int intTimeout = 5; 
    try 
    { 
      HttpURLConnection urlConnection= null; 
      URL url = new URL(strURL); 
      urlConnection=(HttpURLConnection)url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setDoOutput(true); 
      urlConnection.setDoInput(true); 
      urlConnection.setRequestProperty 
      ( 
        "User-Agent","Mozilla/4.0"+ 
        " (compatible; MSIE 6.0; Windows 2000)" 
      ); 
       
      urlConnection.setRequestProperty 
      ("Content-type","text/html; charset="+strEncoding);      
      urlConnection.setConnectTimeout(1000*intTimeout); 
      urlConnection.connect(); 
      if (urlConnection.getResponseCode() == 200) 
      { 
        return true; 
      } 
      else 
      { 
        return false; 
      } 
    } 
    catch (Exception e) 
    { 
      e.printStackTrace(); 
      return false; 
    } 
  }


 有时候google二维码api  http://chart.apis.google.com/chart?chs="+
      strWidth+"x"+strWidth+"&chl 不好用,可能是被墙了。可以用这个代替 http://qrcode.kaywa.com/img.php?s=5&d= (参数d和google里的chs一样)

 

// 判断是否为横屏
		if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
		}
		// 判断是否为竖屏
		else if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
		}


 

你可能感兴趣的:(exception,android,网络,String,Google,url)