获取设备的外部IP会导致异常

时间:2014-08-23 14:57:22

标签: android http ip

我正在尝试获取设备的外部IP。我查看了类似问题的许多答案,发现了许多不同的方法,但所有这些方法都抛出了异常,所以这个问题并不重复。我在清单中添加了以下权限:

<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"/>

我尝试过的解决方案之一如下: 来自(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");
}

}

除了这段代码,我尝试了很多其他不同的网站,但我仍然有例外。我是否需要其他权限或代码有问题?其他人说这段代码对他们有用。请帮忙。

这是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是

response = httpclient.execute(httpget);

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。代码抛出了异常,因为android不允许主线程中的http请求。所以我使用了一个新线程并且代码工作正常。

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

                  //The code from my question here

            });
            thread.start();