Android gets IP from extranet and intranet

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.