0

I am trying to get the external ip of my device. I ve looked at many answers to similar questions and found many different methods but all of them throw exceptions so this question is not a duplicate. I added in the manifest the following permissions :

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

One of the solutions I tried is the following : from (Android get external IP)

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");
}

}

Apart from this code I tried many other with different sites but I am still getting exceptions . Do I need other permissions or is there something wrong with the code ? Other people said that this code worked for them. Please help.

Here is the logcat :

08-23 18:11:52.430: E/MYAPP(9644): exception
08-23 18:11:52.430: E/MYAPP(9644): android.os.NetworkOnMainThreadException
08-23 18:11:52.430: E/MYAPP(9644):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1142)
08-23 18:11:52.430: E/MYAPP(9644):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-23 18:11:52.430: E/MYAPP(9644):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-23 18:11:52.430: E/MYAPP(9644):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-23 18:11:52.430: E/MYAPP(9644):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:141)
08-23 18:11:52.430: E/MYAPP(9644):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-23 18:11:52.430: E/MYAPP(9644):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-23 18:11:52.430: E/MYAPP(9644):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-23 18:11:52.430: E/MYAPP(9644):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-23 18:11:52.430: E/MYAPP(9644):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-23 18:11:52.430: E/MYAPP(9644):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-23 18:11:52.430: E/MYAPP(9644):  at com.mreprogramming.ultimatesmartphoneanalyser.Wifi.external_IP(Wifi.java:95)
08-23 18:11:52.430: E/MYAPP(9644):  at com.mreprogramming.ultimatesmartphoneanalyser.Wifi.onResume(Wifi.java:176)
08-23 18:11:52.430: E/MYAPP(9644):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
08-23 18:11:52.430: E/MYAPP(9644):  at android.app.Activity.performResume(Activity.java:5211)
08-23 18:11:52.430: E/MYAPP(9644):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2844)
08-23 18:11:52.430: E/MYAPP(9644):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2883)
08-23 18:11:52.430: E/MYAPP(9644):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2321)
08-23 18:11:52.430: E/MYAPP(9644):  at android.app.ActivityThread.access$600(ActivityThread.java:150)
08-23 18:11:52.430: E/MYAPP(9644):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
08-23 18:11:52.430: E/MYAPP(9644):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 18:11:52.430: E/MYAPP(9644):  at android.os.Looper.loop(Looper.java:213)
08-23 18:11:52.430: E/MYAPP(9644):  at android.app.ActivityThread.main(ActivityThread.java:5225)
08-23 18:11:52.430: E/MYAPP(9644):  at java.lang.reflect.Method.invokeNative(Native Method)
08-23 18:11:52.430: E/MYAPP(9644):  at java.lang.reflect.Method.invoke(Method.java:525)
08-23 18:11:52.430: E/MYAPP(9644):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
08-23 18:11:52.430: E/MYAPP(9644):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-23 18:11:52.430: E/MYAPP(9644):  at dalvik.system.NativeStart.main(Native Method)

Wifi.java:95 is

response = httpclient.execute(httpget);
Community
  • 1
  • 1
mremremre1
  • 1,036
  • 3
  • 16
  • 34

1 Answers1

0

I solved the problem. The code was throwing exceptions because android doesn't allow http requests in the main thread. So I used a new thread and the code worked.

Thread thread = new Thread(new Runnable(){
              @Override
              public void run(){
                  Looper.prepare(); 

                  //The code from my question here

            });
            thread.start();
mremremre1
  • 1,036
  • 3
  • 16
  • 34
  • why don't you just search exception you get first ? This is something known for years. So it makes your question duplicate. – osayilgan Aug 23 '14 at 16:47
  • @osayilgan you are right I should have checked first. But the answer might help other people since the questions I checked didn't mention new threads. So I believe this question is useful. – mremremre1 Aug 23 '14 at 16:56