Android 查看本机外网IP .

[java] view plain copy print ?
  1. String GetNetIp(String ipaddr){  
  2.      URL infoUrl = null;  
  3.      InputStream inStream = null;  
  4.      try {  
  5.          infoUrl = new URL(ipaddr);  
  6.          URLConnection connection = infoUrl.openConnection();  
  7.          HttpURLConnection httpConnection = (HttpURLConnection)connection;  
  8.          int responseCode = httpConnection.getResponseCode();  
  9.          if(responseCode == HttpURLConnection.HTTP_OK)  
  10.            {      
  11.               inStream = httpConnection.getInputStream();     
  12.             BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8"));  
  13.             StringBuilder strber = new StringBuilder();  
  14.             String line = null;  
  15.             while ((line = reader.readLine()) != null)   
  16.                 strber.append(line + "\n");  
  17.             inStream.close();  
  18.             return strber.toString();               
  19.           }  
  20.      } catch (MalformedURLException e) {  
  21.          // TODO Auto-generated catch block   
  22.          e.printStackTrace();  
  23.      } catch (IOException e) {  
  24.          // TODO Auto-generated catch block   
  25.          e.printStackTrace();  
  26.      }  
  27.     return "";  
  28.  }      


查看 System.out.println((GetNetIp("http://fw.qq.com/ipaddress")));
加权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
通过获取http://fw.qq.com/ipaddress网页取得外网IP

你可能感兴趣的:(java,android,String,null,url)