可能重复:
如何获取设备的 IP 地址?
下面是我如何尝试获取外部 IP 的片段。但是,它不会返回任何东西......
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;
}