android获取公网ip地址,获取公网ip
/*** 获取公网ip* @return*/public String getNetIp() {String ip = "";InputStream inStream = null;try {URL infoUrl = new URL("http://1212.ip138.com/ic.asp");URLConnection connection = infoUrl.openConnection(
/**
* 获取公网ip
* @return
*/
public String getNetIp() {
String ip = "";
InputStream inStream = null;
try {
URL infoUrl = new URL("http://1212.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, "gb2312"));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
//builder.append(line).append("\n");
}
inStream.close();
int start = builder.indexOf("[");
int end = builder.indexOf("]");
ip = builder.substring(start + 1, end);
return ip;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Sharezer , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明获取公网ip!
更多推荐
所有评论(0)