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 ;}