0

ローカルではない isp から提供されたデバイスの IP アドレスを読み取ろうとしています。

コードからデバイスのIPアドレスを取得するには? このチェック済みの回答では、WI-FI の LAN IP アドレスが表示され、セル インターネットの whatismyip とは異なる IP アドレスが表示されます。オペレーターサービスプロバイダーのローカルIPである可能性があります。

whatismyip を返すインターネット IP アドレスを取得するにはどうすればよいですか?

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 Edit: 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 に答える