Android obtains Internet and Intranet IP addresses.
Very late. Live code:
/*** Get the Internet IP address (to access the Url, put it in the background thread for processing) ** @ Title: GetNetIp * @ Description: * @ param @ return * @ return String * @ throws */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; in T 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 ** @ return */public static String getLocalIpAddress () {try {Enumeration
En = NetworkInterface. getNetworkInterfaces (); while (en. hasMoreElements () {NetworkInterface ni = en. nextElement (); Enumeration
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 ";}