There are many examples of IP access, but all of the local IP is acquired, there are some because the access to the IP is empty, the following see the actual measurement to obtain the code of the external network IP, note the need to execute inside the thread
/*** Get the IP of the extranet (to access the URL, to be placed in the background line thread processing) * *@param @return * @returnString *@throws* @Title: Getnetip * @Description:*/ Public StaticString Getnetip () {URL Infourl=NULL; InputStream instream=NULL; String Ipline= ""; HttpURLConnection httpconnection=NULL; Try {//Infourl = new URL ("http://ip168.com/");Infourl =NewURL ("Http://pv.sohu.com/cityjson?ie=utf-8"); URLConnection Connection=infourl.openconnection (); Httpconnection=(httpurlconnection) connection; intResponsecode =Httpconnection.getresponsecode (); if(Responsecode = =HTTPURLCONNECTION.HTTP_OK) {instream=Httpconnection.getinputstream (); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (instream, "Utf-8")); StringBuilder Strber=NewStringBuilder (); 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 (); } Catch(Exception ex) {ex.printstacktrace (); }} log.e ("Getnetip", Ipline); returnIpline; }
Android gets an external IP, measured effectively