Как получить внешний IP Андроид устройства
public void getCurrentIP () {
ip.setText("Please wait...");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://ifcfg.me/ip");
// HttpGet httpget = new HttpGet("http://ipecho.net/plain");
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");
}
}
это не хороший вариант
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;
}
public void getCurrentIP () {
ip.setText("Please wait...");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://ifcfg.me/ip");
// HttpGet httpget = new HttpGet("http://ipecho.net/plain");
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");
}
}
это не хороший вариант
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;
}
Комментариев нет:
Отправить комментарий