2012-10-12 10 views
1

가능한 중복 : 아래
How to get ip address of the device?어떻게 외부/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; 

} 
+0

로그 고양이는 어디에 있습니까? –

답변

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

또는

UPDATE 2015년 2월

을 가질 수 있습니다

WhatIsMyIp는 이제 사용할 수있는 developer API을 노출합니다.

+0

<사용 허가 android : name = "android.permission.INTERNET"/> –

+0

고맙습니다 !!!! 오늘 아침 이후로 붙어 있습니다 .. 끝내 주셨습니다. : = – omi0301

+0

언제든지 물어보십시오. 안드로이드 - PHP 문제. 감사합니다 –

관련 문제