/** * Get extranet IP */public static String getnetip () {URL Infourl = Null;inputstream instream = null; String ipline = ""; HttpURLConnection httpconnection = null;try {infourl = new URL ("http://ip168.com/"); URLConnection connection = Infourl.openconnection (); 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"); Pattern pattern = Pattern.compile ("(?:(? : 25[0-5]|2[0-4]\\d| ((1\\d{2}) | ([1-9]?\\d))) \\.) {3} (?: 25[0-5]|2[0-4]\\d| ((1\\d{2}) | ([1-9]?\\d)))); Matcher Matcher = Pattern.matcher (strber.tostring ()), if (Matcher.find ()) {ipline = Matcher.group ();}}} catch (Malformedurlexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Finally {try {instream.close (); Httpconnection.disconnect ();} catch (IOException e) {e.printstacktrace ()}} return ipline;} /** * Get local IP */public static String getlocalipaddress () {try {enumeration<networkinterface> en = networkinterface.g Etnetworkinterfaces (); while (En.hasmoreelements ()) {NetworkInterface ni = en.nextelement (); enumeration<inetaddress> Enip = ni.getinetaddresses (); while (Enip.hasmoreelements ()) {InetAddress inet = Enip.nextelement (); if (!inet.isloopbackaddress () && (inet instanceof inet4address)) {return Inet.gethostaddress (). toString ();}}} catch (SocketException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return "0";}
How Android acquires extranet and local IP