Very late, live on the code:
/** * Obtain the IP of the extranet (to access the URL, to be placed in the background line thread processing) * * @Title: Getnetip * @Description: * @param @return * @return String * @throws */publi C 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 {inStr Eam.close (); Httpconnection.disconnect ();} catch (IOException e) {e.printstacktrace ();}} return ipline;} /** * Get Local IP * * @return */public static String getlocalipaddress () {try {enumeration<networkinterface> en = networ Kinterface.getnetworkinterfaces (); 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";}
Android gets IP from extranet and intranet