在应用调试过程中,对接的技术人员要求提供一下机顶盒 的外网IP地址,于是打开了盒子上的设置,将IP地址发了过去,对面回复说这是私网IP,要公网IP才可以。
一般情况下,电脑的公网IP地址很好查:打开百度:
但是有些机顶盒上是没有浏览器的,所以就需要通过代码的层面来获取。
通过百度,先找到了如下的方法:
/**
* 获取IP地址
* @return
* @throws SocketException
*/
public static String getLocalIPAddress() throws SocketException {
for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){
NetworkInterface intf = en.nextElement();
for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){
InetAddress inetAddress = enumIpAddr.nextElement();
if(!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)){
return inetAddress.getHostAddress().toString();
}
}
}
return "null";
}
但是获取到的结果和机顶盒设置里的ip一致的,也就是说还是一个私网IP。
后经过查询,可通过如下方式获取公网IP:
/**
* 获取外网IP地址
* @return
*/
public static String GetNetIp(){
String IP = "";
try {
String address = "http://ip.taobao.com/service/getIpInfo2.php?ip=myip";
URL url = new URL(address);
//URLConnection htpurl=url.openConnection();
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setUseCaches(false);
connection.setRequestMethod("GET");
connection.setRequestProperty("user-agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.7 Safari/537.36"); //设置浏览器ua 保证不出现503
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream in = connection.getInputStream();
// 将流转化为字符串
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String tmpString = "";
StringBuilder retJSON = new StringBuilder();
while ((tmpString = reader.readLine()) != null) {
retJSON.append(tmpString + "\n");
}
JSONObject jsonObject = new JSONObject(retJSON.toString());
String code = jsonObject.getString("code");
if (code.equals("0")) {
JSONObject data = jsonObject.getJSONObject("data");
IP = data.getString("ip") + "(" + data.getString("country")
+ data.getString("area") + "区"
+ data.getString("region") + data.getString("city")
+ data.getString("isp") + ")";
Log.e("提示", "您的IP地址是:" + IP);
} else {
IP = "";
Log.e("提示", "IP接口异常,无法获取IP地址!");
}
} else {
IP = "";
Log.e("提示", "网络连接异常,无法获取IP地址!");
}
} catch (Exception e) {
IP = "";
Log.e("提示", "获取IP地址时出现异常,异常信息是:" + e.toString());
}
return IP;
}
在代码中调用:
try {
String localIPAddress = IPutils.getLocalIPAddress();
LogUtil.Log_i("IP地址:"+localIPAddress);
} catch (SocketException e) {
e.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
String s = IPutils.GetNetIp();
LogUtil.Log_i("外网IP:"+s);
}
}).start();
获取到的结果:
外网IP:221.220.160.241(中国区北京北京联通)
这说明是成功的。需要注意的是,获取外网IP的方法必须要在子线程中执行。
附完整的工具类:
package com.zyp.river.utils;
import android.util.Log;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
/**
* Created by ZhangYanPeng on 2018/9/19.
*/
public class IPutils {
/**
* 获取IP地址
* @return
* @throws SocketException
*/
public static String getLocalIPAddress() throws SocketException {
for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){
NetworkInterface intf = en.nextElement();
for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){
InetAddress inetAddress = enumIpAddr.nextElement();
if(!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)){
return inetAddress.getHostAddress().toString();
}
}
}
return "null";
}
/**
* 获取外网IP地址
* @return
*/
public static String GetNetIp(){
String IP = "";
try {
String address = "http://ip.taobao.com/service/getIpInfo2.php?ip=myip";
URL url = new URL(address);
//URLConnection htpurl=url.openConnection();
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setUseCaches(false);
connection.setRequestMethod("GET");
connection.setRequestProperty("user-agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.7 Safari/537.36"); //设置浏览器ua 保证不出现503
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream in = connection.getInputStream();
// 将流转化为字符串
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String tmpString = "";
StringBuilder retJSON = new StringBuilder();
while ((tmpString = reader.readLine()) != null) {
retJSON.append(tmpString + "\n");
}
JSONObject jsonObject = new JSONObject(retJSON.toString());
String code = jsonObject.getString("code");
if (code.equals("0")) {
JSONObject data = jsonObject.getJSONObject("data");
IP = data.getString("ip") + "(" + data.getString("country")
+ data.getString("area") + "区"
+ data.getString("region") + data.getString("city")
+ data.getString("isp") + ")";
Log.e("提示", "您的IP地址是:" + IP);
} else {
IP = "";
Log.e("提示", "IP接口异常,无法获取IP地址!");
}
} else {
IP = "";
Log.e("提示", "网络连接异常,无法获取IP地址!");
}
} catch (Exception e) {
IP = "";
Log.e("提示", "获取IP地址时出现异常,异常信息是:" + e.toString());
}
return IP;
}
}