0

我正在尝试读取来自 isp 的非本地设备 IP 地址。

如何从代码中获取设备的 IP 地址?在这个检查过的答案中,我给出了 WI-FI 的 LAN IP 地址,并给出了与蜂窝互联网上的 whatismyip 不同的 IP 地址。我猜它可能是运营商服务提供商的本地IP。

我如何才能上网 IP 地址返回 whatismyip 的内容?

public static String getPublicIP() throws IOException
{
    Document doc = Jsoup.connect("http://www.checkip.org").get();
    return doc.getElementById("yourip").select("h1").first().select("span").text();
}

这个可能有用,但我不想为此添加任何库。

smallBig 编辑: http ://api.exip.org/?call= ip 它只返回 ip,我该如何使用它?它可靠且寿命长吗?

4

2 回答 2

0

如果您不想添加库,请使用URLConnection并自己进行解析。

于 2013-05-31T08:12:56.807 回答
-1
public void getCurrentIP () {
  ip.setText("Please wait...");  
  try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://whatismyip.everdot.org/ip");
        // 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();
        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");
  }

}
于 2013-05-31T08:13:52.397 回答