我怎样才能得到我的外部/IP? [复制]
Posted
技术标签:
【中文标题】我怎样才能得到我的外部/IP? [复制]【英文标题】:how can i get my external / ip? [duplicate] 【发布时间】:2012-10-02 23:50:43 【问题描述】:可能重复:How to get ip address of the device?
下面是我如何尝试获取外部 IP 的 sn-p。但是,它没有返回任何东西......
public String getIpAddress()
try
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.whatismyip.com/?404");
// HttpGet httpget = new HttpGet("http://whatismyip.com.au/");
// HttpGet httpget = new HttpGet("http://www.whatismyip.org/");
HttpResponse response;
response = httpclient.execute(httpget);
//Log.i("externalip",response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
entity.getContentLength();
str = EntityUtils.toString(entity);
catch (Exception e)
return str;
【问题讨论】:
【参考方案1】:public String getIpAddress()
String ip;
try
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
// HttpGet httpget = new HttpGet("http://whatismyip.com.au/");
// HttpGet httpget = new HttpGet("http://www.whatismyip.org/");
HttpResponse response;
response = httpclient.execute(httpget);
//Log.i("externalip",response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
entity.getContentLength();
str = EntityUtils.toString(entity);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
JSONObject json_data = new JSONObject(str);
ip = json_data.getString("ip");
Toast.makeText(getApplicationContext(), ip, Toast.LENGTH_LONG).show();
catch (Exception e)...
return ip;
或
2015 年 2 月更新
WhatIsMyIp 现在公开了一个您可以使用的developer API。
【讨论】:
你可以看看这里 http://code.google.com/p/external-ip/source/browse/src/org/kost/externalip/ExternalIP.java
还有这里 External ip address in android programatically
【讨论】:
以上是关于我怎样才能得到我的外部/IP? [复制]的主要内容,如果未能解决你的问题,请参考以下文章