
方法是利用网址:http://pv.sohu.com/cityJson?IE=utf-8,返回String类型的ip地址:
public static String getNetIp() { String ip = ""; inputStream inputStream = null; try { URL infoUrl = new URL("http://pv.sohu.com/cityJson?IE=utf-8"); URLConnection connection = infoUrl.openConnection(); httpURLConnection httpconnection = (httpURLConnection) connection; int responseCode = httpconnection.getResponseCode(); if (responseCode == httpURLConnection.http_OK) { inputStream = httpconnection.getinputStream(); BufferedReader reader = new BufferedReader(new inputStreamReader(inputStream,"gb2312")); StringBuilder builder = new StringBuilder(); String line = null; while ((line = reader.readline()) != null) { builder.append(line); } inputStream.close(); int start1=builder.indexOf(":"); String str1=builder.substring(start1+1); int start2=str1.indexOf("\""); String str2=str1.substring(start2+1); int start3=str2.indexOf("\""); ip=str2.substring(0,start3); return ip; } } catch (IOException e) { e.printstacktrace(); } return null; }
注意用到网络连接的方法不能用在主线程
总结以上是内存溢出为你收集整理的【android】获取本机ip地址全部内容,希望文章能够帮你解决【android】获取本机ip地址所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)