2

重複の可能性:
デバイスの 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;

}
4

2 に答える 2

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;
}

また

2015年2月更新

WhatIsMyIp は、使用できる開発者 APIを公開するようになりました。

于 2012-10-12T08:22:42.970 に答える