您的位置:首页 > 移动开发 > Android开发

Android 查看本机外网IP

2011-07-07 20:10 357 查看
String GetNetIp(String ipaddr){
URL infoUrl = null;
InputStream inStream = null;
try {
infoUrl = new URL(ipaddr);
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();
return strber.toString();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}

查看 System.out.println((GetNetIp("http://fw.qq.com/ipaddress")));
加权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
通过获取http://fw.qq.com/ipaddress网页取得外网IP
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: