Android 查看本機外網IP

 
   String GetNetIp(String ipaddr){
        URL infoUrl = null;
        InputStream inStream = null;
        try {
            infoUrl = new URL(ipaddr);
            URLConnection connection = infoUrl.openConnection();
            HttpURLConnection httpConnection = (HttpURLConnection)connection;
            int responseCode = httpConnection.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK)
              {    
                 inStream = httpConnection.getInputStream();   
               BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8"));
               StringBuilder strber = new StringBuilder();
               String line = null;
               while ((line = reader.readLine()) != null) 
                   strber.append(line + "\n");
               inStream.close();
               return strber.toString();             
             }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       return "";
    }    


查看 System.out.println((GetNetIp("http://fw.qq.com/ipaddress")));
加權限 <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
通過獲取http://fw.qq.com/ipaddress網頁取得外網IP

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章