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