我怎样才能得到我的外部/ IP?

omi*_*301 2 ip android http

可能重复:
如何获取设备的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;

}
Run Code Online (Sandbox Code Playgroud)

Nir*_*ara 12

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;
}
Run Code Online (Sandbox Code Playgroud)

要么

2015年2月更新

WhatIsMyIp现在公开了一个可以使用的开发人员API.