Android获得外部IP
我正在android 2.1中开发一个应用程序,我想显示外部IP。我怎么能这样做?提前感谢。我不认为有一种方法可以通过编程实现,但是你可以调用类似的站点,然后从页面中删除IP。您可能希望找到一个提供API并支持第三方调用的站点。我认为没有一种方法可以通过编程实现,但您可以调用类似的站点,然后从页面中删除IP。您可能希望找到一个提供API并支持第三方调用的站点。Android获得外部IP,android,ip,external,Android,Ip,External,我正在android 2.1中开发一个应用程序,我想显示外部IP。我怎么能这样做?提前感谢。我不认为有一种方法可以通过编程实现,但是你可以调用类似的站点,然后从页面中删除IP。您可能希望找到一个提供API并支持第三方调用的站点。我认为没有一种方法可以通过编程实现,但您可以调用类似的站点,然后从页面中删除IP。您可能希望找到一个提供API并支持第三方调用的站点。public void getCurrentIP(){ public void getCurrentIP () { ip.setT
public void getCurrentIP(){
public void getCurrentIP () {
ip.setText("Please wait...");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://ifcfg.me/ip");
// HttpGet httpget = new HttpGet("http://ipecho.net/plain");
HttpResponse response;
response = httpclient.execute(httpget);
//Log.i("externalip",response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
if (len != -1 && len < 1024) {
String str=EntityUtils.toString(entity);
//Log.i("externalip",str);
ip.setText(str);
} else {
ip.setText("Response too long or error.");
//debug
//ip.setText("Response too long or error: "+EntityUtils.toString(entity));
//Log.i("externalip",EntityUtils.toString(entity));
}
} else {
ip.setText("Null:"+response.getStatusLine().toString());
}
}
catch (Exception e)
{
ip.setText("Error");
}
}
ip.setText(“请稍候…”);
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(“http://ifcfg.me/ip");
//HttpGet HttpGet=新的HttpGet(“http://ipecho.net/plain");
HttpResponse响应;
response=httpclient.execute(httpget);
//Log.i(“externalip”,response.getStatusLine().toString());
HttpEntity=response.getEntity();
如果(实体!=null){
long len=entity.getContentLength();
如果(len!=-1&&len<1024){
String str=EntityUtils.toString(实体);
//Log.i(“外部性”,str);
ip.setText(str);
}否则{
setText(“响应太长或错误”);
//调试
//ip.setText(“响应太长或错误:+EntityUtils.toString(实体));
//Log.i(“externalip”,EntityUtils.toString(entity));
}
}否则{
setText(“Null:+response.getStatusLine().toString());
}
}
捕获(例外e)
{
ip.setText(“错误”);
}
}
公共无效getCurrentIP(){
ip.setText(“请稍候…”);
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(“http://ifcfg.me/ip");
//HttpGet HttpGet=新的HttpGet(“http://ipecho.net/plain");
HttpResponse响应;
response=httpclient.execute(httpget);
//Log.i(“externalip”,response.getStatusLine().toString());
HttpEntity=response.getEntity();
如果(实体!=null){
long len=entity.getContentLength();
如果(len!=-1&&len<1024){
String str=EntityUtils.toString(实体);
//Log.i(“外部性”,str);
ip.setText(str);
}否则{
setText(“响应太长或错误”);
//调试
//ip.setText(“响应太长或错误:+EntityUtils.toString(实体));
//Log.i(“externalip”,EntityUtils.toString(entity));
}
}否则{
setText(“Null:+response.getStatusLine().toString());
}
}
捕获(例外e)
{
ip.setText(“错误”);
}
}
将以简单的api格式返回您的ip
您可以在此处阅读有关获取外部ip的更多信息:将以简单的api格式返回您的ip
您可以在此处阅读有关获取外部ip的更多信息:查看以下代码片段:
String ipAddress = null;
try {
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()) {
ipAddress = inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
Log.e("IP ADDRESS:", ipAddress);
String ipAddress=null;
试一试{
对于(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){
InetAddress InetAddress=enumIpAddr.nextElement();
如果(!inetAddress.isLoopbackAddress()){
ipAddress=inetAddress.getHostAddress().toString();
}
}
}
}捕获(SocketException例外){
例如printStackTrace();
}
Log.e(“IP地址:”,ipAddress);
看看这个代码片段:
String ipAddress = null;
try {
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()) {
ipAddress = inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
Log.e("IP ADDRESS:", ipAddress);
String ipAddress=null;
试一试{
对于(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){
InetAddress InetAddress=enumIpAddr.nextElement();
如果(!inetAddress.isLoopbackAddress()){
ipAddress=inetAddress.getHostAddress().toString();
}
}
}
}捕获(SocketException例外){
例如printStackTrace();
}
Log.e(“IP地址:”,ipAddress);
<代码>你认为什么是“外部”IP地址?你认为什么是“外部”IP地址?很好:但是在AsyncTask中使用这个url。如果你想要json数据,那真是太棒了,谢谢你和大家分享这个链接。很好:但是在AsyncTask中使用这个url。如果你想要json数据,那真是太棒了,谢谢你与所有人共享链接。获取本地IP而不是外部IP获取本地IP而不是外部IP