0

I Couldn't get any IP when I run the following method:

System.out.println("IP: " +getIpAddress());
public String getIpAddress() 
 {
   String ip = null;
   try 
   {
      HttpClient httpclient = new DefaultHttpClient();
      //HttpGet httpget = new HttpGet("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
      HttpGet httpget = new HttpGet("http://agentgatech.appspot.com/");

      HttpResponse response;
      System.out.println("IP: 1");
      response = httpclient.execute(httpget);
      System.out.println("IP: 2" +response.getStatusLine().toString());

      HttpEntity entity = response.getEntity();
      entity.getContentLength();
      String str = EntityUtils.toString(entity);
      System.out.println("IP 3" +getApplicationContext() +str);
      JSONObject json_data = new JSONObject(str);
      ip = json_data.getString("ip");
      System.out.println("IP 4" +getApplicationContext() +ip);
   }
       catch (Exception e)
      {

      }

   return ip;
 }

I have already added thw following to the manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

I just got the following printed out!:

    IP: 1
    IP: null

Can any one tell me why? where is the wrong in this code?

Edit: Just for the others, the answer is by using thread as shown bellow:

Thread thread = new Thread(new Runnable(){
@Override
public void run() {
    try {
        HttpClient httpclient = new DefaultHttpClient();
       // HttpGet httpget = new HttpGet("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
        HttpGet httpget = new HttpGet("http://agentgatech.appspot.com/");

        HttpResponse response;
        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        entity.getContentLength();
        String str = EntityUtils.toString(entity);
        System.out.println("External IP: " +str);

        } catch (Exception e) {
        e.printStackTrace();
    }
    }
   });

To start it:

thread.start();
Majid ff
  • 249
  • 7
  • 22
  • Please log the exception rather than ignoring it. – CommonsWare Feb 15 '15 at 20:11
  • I didn't get any exception! or stopping the app! – Majid ff Feb 15 '15 at 20:17
  • That is because you are not logging the exception, but instead are ignoring it in your `catch` block. Clearly, you are getting an exception, otherwise your other IP lines would be showing in LogCat. – CommonsWare Feb 15 '15 at 20:24
  • @CommonsWare Thanks for your useful comment, I added the exception catch (IOException e){e.printStackTrace();} and I got this error which cause stopping the app: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testttt/com.example.testttt.MainActivity}: android.os.NetworkOnMainThreadException – Majid ff Feb 15 '15 at 20:37
  • Ip address is already here: `String str = EntityUtils.toString(entity);` – Taras B Feb 15 '15 at 21:05
  • Ok thanks for every one, I solved the proble by put the code inside thread it is work now: Thread thread = new Thread(new Runnable(){ @Override public void run() { try { //Your code goes here } catch (Exception e) { e.printStackTrace(); } } }); thread.start(); – Majid ff Feb 15 '15 at 21:09

0 Answers0