1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android公网ip android 获取公网ip 内网ip

android公网ip android 获取公网ip 内网ip

时间:2020-04-28 09:17:35

相关推荐

android公网ip android 获取公网ip 内网ip

/**

* 获取公网ip

* @return

*/

public String getNetIp() {

URL infoUrl = null;

InputStream inStream = null;

try {

infoUrl = new URL("/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 strber = new StringBuilder();

String line = null;

while ((line = reader.readLine()) != null)

strber.append(line + "\n");

inStream.close();

System.out.println("net-result----->"+strber);

//从反馈的结果中提取出IP地址

int start = strber.indexOf("[");

int end = strber.indexOf("]", start + 1);

line = strber.substring(start + 1, end);

return line;

}

}

catch(MalformedURLException e) {

e.printStackTrace();

}

catch (IOException e) {

e.printStackTrace();

}

return null;

}

/**

* 获取内网ip

* @return

*/

private String getLocalIPAddress(){

try {

for(Enumeration en = NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){

NetworkInterface intf = en.nextElement();

for(Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){

InetAddress inetAddress = enumIpAddr.nextElement();

if(!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)){

return inetAddress.getHostAddress().toString();

}

}

}

} catch (SocketException e) {

e.printStackTrace();

}

return "null";

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。