How to obtain the Internet ip address of wifi for android

Source: Internet
Author: User
Tags get ip

How to obtain the Internet ip address of wifi for android

// Get the Internet IPpublic static String GetNetIp () {URL infoUrl = null; InputStream inStream = null; try {// http://iframe.ip138.com/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 (responseCod E = 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 +); inStream. close (); // extract the IP address from the feedback result // 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 ;}

Another high-end method for obtaining Internet IP addresses

 

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 (); // convert stream to String BufferedReader reader = new BufferedReader (new InputStreamReader (in); String tmpString =; StringBuilder retJSON = new StringBuilder (); while (tm PString = reader. readLine ())! = Null) {retJSON. append (tmpString +);} JSONObject jsonObject = new JSONObject (retJSON. toString (); String code = jsonObject. getString (code); if (code. equals (0) {JSONObject data = jsonObject. getJSONObject (data); IP = data. getString (ip) + (+ data. getString (country) + data. getString (area) + area + data. getString (region) + data. getString (city) + data. getString (isp) +); Log. e (prompt, your IP address is: + IP);} else {IP =; Log. e (Prompt, IP interface exception, unable to get IP address !);}} Else {IP =; Log. e (prompt, network connection exception, unable to obtain IP address !);}} Catch (Exception e) {IP =; Log. e (Note: An Exception occurred when obtaining the IP address. The Exception message is + e. toString ();} return IP ;}


 

 

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.