How to get WiFi extranet IP from Android

Source: Internet
Author: User
Tags get ip

Tag:wifi    Extranet IP access     get extranet ip   

<span style= "FONT-SIZE:18PX;" >//get extranet ippublic static String Getnetip () {URL Infourl = Null;inputstream instream = null;try {//HTTP://IFRAME.IP138.C om/ic.asp//infourl = new URL ("http://city.ip138.com/city0.asp"); infourl = new URL ("http://ip38.com"); URLConnection connection = Infourl.openconnection (); HttpURLConnection 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"), Instream.close ();//extract IP ground from the results of the feedback Address//int start = Strber.indexof ("[");//LOG.D ("Zph", "" "+ start);//int end = Strber.indexof ("] ", start + 1);//LOG.D (" Zph " , "+ End", line = strber.substring (378, 395), Line.replaceall ("", ""); return line;}} catch (MalformedurlexceptiOn e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return null;} </span>

Another high-end approach to acquiring an extranet IP

public static string Getnetip () {String IP = ""; try{string address = "Http://ip.taobao.com/service/getIpInfo2.php?ip=myip"; URL url = new URL (address); HttpURLConnection connection = (httpurlconnection) url.openconnection (); connection.setusecaches (false); if ( Connection.getresponsecode () = = HTTPURLCONNECTION.HTTP_OK) {InputStream in = Connection.getinputstream ();// Converts the stream into a string BufferedReader reader = new BufferedReader (new InputStreamReader (in)); String tmpstring = ""; StringBuilder Retjson = new StringBuilder (); while ((tmpstring = Reader.readline ()) = null) {retjson.append (tmpstring + "\ n ");} Jsonobject jsonobject = new Jsonobject (retjson.tostring ()); String code = jsonobject.getstring ("code"), if (Code.equals ("0")) {Jsonobject data = Jsonobject.getjsonobject ("Data"); P = data.getstring ("IP") + "(" + data.getstring ("country") + data.getstring ("area") + "zone" + data.getstring ("region") + data. GetString ("city") + data.getstring ("ISP") + ")"; LOG.E ("Prompt", "Your IP address is:" + IP);} Else{ip = ""; LOG.E ("Hint", "IP interface exception, unable to obtainTake the IP address! ");}} Else{ip = ""; LOG.E ("Hint", "network connection is abnormal, unable to get IP address!") ");}} catch (Exception e) {IP = ""; LOG.E ("Hint", "an exception occurred when getting IP address, exception information is:" + e.tostring ());} return IP;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How to get WiFi extranet IP from Android

Related Article

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.