我想要获取公共IP地址,就像从whatismyipaddress.com获取的那样,而且我不想使用第三方服务https://www.ipify.org。有没有办法从Android内置的类或库中获取此IP地址。
如果无法从Android库中获取所需数据,请建议在服务器级别获取相同数据的逻辑,而不是在客户端级别获取。注意:负载均衡器存在于路由到实际Web应用程序之前。
我想要获取公共IP地址,就像从whatismyipaddress.com获取的那样,而且我不想使用第三方服务https://www.ipify.org。有没有办法从Android内置的类或库中获取此IP地址。
如果无法从Android库中获取所需数据,请建议在服务器级别获取相同数据的逻辑,而不是在客户端级别获取。注意:负载均衡器存在于路由到实际Web应用程序之前。
我在我的项目中使用这种方法,所以你也可以使用它。它将返回您的设备IP地址。
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
如果您想获取您的设备连接的网络的公共IP,请使用以下代码...
public class GetPublicIP extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... strings) {
String publicIP = "";
try {
java.util.Scanner s = new java.util.Scanner(
new java.net.URL(
"https://api.ipify.org")
.openStream(), "UTF-8")
.useDelimiter("\\A");
publicIP = s.next();
System.out.println("My current IP address is " + publicIP);
} catch (java.io.IOException e) {
e.printStackTrace();
}
return publicIP;
}
@Override
protected void onPostExecute(String publicIp) {
super.onPostExecute(publicIp);
Log.e("PublicIP", publicIp+"");
//Here 'publicIp' is your desire public IP
}
}
新执行它
new GetPublicIP().execute();
100.0
当然也是公共范围,只需禁用移动数据并使用 WiFi 即可。 - Martin Zeitlerprivate suspend fun getMyPublicIpAsync() : Deferred<String> =
coroutineScope {
async(Dispatchers.IO) {
var result = ""
result = try {
val url = URL("https://api.ipify.org")
val httpsURLConnection = url.openConnection()
val iStream = httpsURLConnection.getInputStream()
val buff = ByteArray(1024)
val read = iStream.read(buff)
String(buff,0, read)
} catch (e: Exception) {
"error : $e"
}
return@async result
}
}
private fun myFunction() {
CoroutineScope(Dispatchers.Main).launch {
val myPublicIp = getMyPublicIpAsync().await()
Toast.makeText(this@MainActivity, myPublicIp, Toast.LENGTH_LONG).show()
}
}
public static String getIPAddress(boolean useIPv4) {
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
for (InetAddress addr : addrs) {
if (!addr.isLoopbackAddress()) {
String sAddr = addr.getHostAddress();
boolean isIPv4 = sAddr.indexOf(':')<0;
if (useIPv4) {
if (isIPv4)
return sAddr;
} else {
if (!isIPv4) {
int delim = sAddr.indexOf('%');
return delim<0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase();
}
}
}
}
}
} catch (Exception ignored) { }
return "";
}
var ip: String? = null
val thread = Thread {
try {
val url = URL("https://api.ipify.org")
val connection = url.openConnection()
connection.setRequestProperty("User-Agent", "Mozilla/5.0") // Set a User-Agent to avoid HTTP 403 Forbidden error
val inputStream = connection.getInputStream()
val s = java.util.Scanner(inputStream, "UTF-8").useDelimiter("\\A")
ip = s.next()
inputStream.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
thread.start()
String ip;
Thread thread = new Thread(() -> {
try {
URL url = new URL("https://api.ipify.org");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0"); // Set a User-Agent to avoid HTTP 403 Forbidden error
try (Scanner s = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\\A")) {
ip = s.next();
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
});
thread.start();
我在我的Medium帖子Android — 使用Java和Kotlin获取公共IP地址中解释了每一行代码,有兴趣的人可以从那里阅读。