怎么获取外网ip地址

零度殇 2016-09-19 01:55:30

URL infoUrl = null;
InputStream inStream = null;
try {
// http://iframe.ip138.com/ic.asp
infoUrl = new URL("http://city.ip138.com/ip2city.asp");
// infoUrl = new URL("http://iframe.ip138.com/ic.asp");
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();
// 从反馈的结果中提取出IP地址
int start = strber.indexOf("[");
int end = strber.indexOf("]", start + 1);
line = strber.substring(start + 1, end);
Log.e("ip地址", line);
return line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;

网上查到的代码 http://city.ip138.com/ip2city.asp 这个网址可以打开并获取ip
但是代码中 responseCode返回的值是 503
...全文
784 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
零度殇 2016-09-19
  • 打赏
  • 举报
回复
引用 3 楼 dalor 的回复:
它返回的是一个html页面,你在页面代码中找你需要的不就行了。
问题解决了 谢谢 这个网址更简洁一些 可以显示ip 和 地址 http://1212.ip138.com/ic.asp
开发者_android 2016-09-19
  • 打赏
  • 举报
回复
它返回的是一个html页面,你在页面代码中找你需要的不就行了。
零度殇 2016-09-19
  • 打赏
  • 举报
回复
引用 1 楼 dalor 的回复:
安全权限问题。 可以换一个,比如:http://www.cmyip.com 这个页面中的内容太多,你得过滤数据。
这个数据过滤怎么弄。。。 网上查了下 还是不懂。。
开发者_android 2016-09-19
  • 打赏
  • 举报
回复
安全权限问题。 可以换一个,比如:http://www.cmyip.com 这个页面中的内容太多,你得过滤数据。

80,469

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧