InetAddress类在Android中 [英] InetAddress in Android

查看:229
本文介绍了InetAddress类在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打印在Android的网站的IP地址?我可以运行的InetAddress并在netbean使用System.out.println()打印。下面是我的示例代码。

 公共字符串getHostAddress(){
        InetAddress类地址= NULL;
        尝试 {
            ADDR = InetAddress.getByName(www.google.com);
        }

        赶上(UnknownHostException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
            }
        返回addr.getHostAddress();
    }
 

它总是表现出遗憾的是,你的程序已经停止。 可以知道有没有什么办法让访问网站的IP地址的Andr​​oid?

  05-19 14:22:39.008:I / dalvikvm(1062):主题ID = 3:反应信号3
05-19 14:22:39.049:I / dalvikvm(1062):写的堆栈跟踪到/data/anr/traces.txt
05-19 14:22:39.688:I / dalvikvm(1062):主题ID = 3:反应信号3
05-19 14:22:39.828:I / dalvikvm(1062):写的堆栈跟踪到/data/anr/traces.txt
05-19 14:22:39.929:D / AndroidRuntime(1062):关闭虚拟机
05-19 14:22:39.948:W / dalvikvm(1062):主题ID = 1:螺纹退出与未捕获的异常(组= 0x409c01f8)
05-19 14:22:40.039:E / AndroidRuntime(1062):致命异常:主要
05-19 14:22:40.039:E / AndroidRuntime(1062):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.android.destinationurl / com.android.destinationurl.DestinationURL}:android.os.NetworkOnMainThreadException
05-19 14:22:40.039:E / AndroidRuntime(1062):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-19 14:22:40.039:E / AndroidRuntime(1062):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-19 14:22:40.039:E / AndroidRuntime(1062):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
05-19 14:22:40.039:E / AndroidRuntime(1062):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
05-19 14:22:40.039:E / AndroidRuntime(1062):在android.os.Handler.dispatchMessage(Handler.java:99)
05-19 14:22:40.039:E / AndroidRuntime(1062):在android.os.Looper.loop(Looper.java:137)
05-19 14:22:40.039:E / AndroidRuntime(1062):在android.app.ActivityThread.main(ActivityThread.java:4424)
05-19 14:22:40.039:E / AndroidRuntime(1062):在java.lang.reflect.Method.invokeNative(本机方法)
05-19 14:22:40.039:E / AndroidRuntime(1062):在java.lang.reflect.Method.invoke(Method.java:511)
05-19 14:22:40.039:E / AndroidRuntime(1062):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
05-19 14:22:40.039:E / AndroidRuntime(1062):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-19 14:22:40.039:E / AndroidRuntime(1062):在dalvik.system.NativeStart.main(本机方法)
05-19 14:22:40.039:E / AndroidRuntime(1062):由:android.os.NetworkOnMainThreadException
05-19 14:22:40.039:E / AndroidRuntime(1062):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-19 14:22:40.039:E / AndroidRuntime(1062):在java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-19 14:22:40.039:E / AndroidRuntime(1062):在java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-19 14:22:40.039:E / AndroidRuntime(1062):在java.net.InetAddress.getByName(InetAddress.java:295)
05-19 14:22:40.039:E / AndroidRuntime(1062):在com.android.destinationurl.DestinationURL.getHostAddress(DestinationURL.java:57)
05-19 14:22:40.039:E / AndroidRuntime(1062):在com.android.destinationurl.DestinationURL.onCreate(DestinationURL.java:40)
05-19 14:22:40.039:E / AndroidRuntime(1062):在android.app.Activity.performCreate(Activity.java:4465)
05-19 14:22:40.039:E / AndroidRuntime(1062):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-19 14:22:40.039:E / AndroidRuntime(1062):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-19 14:22:40.039:E / AndroidRuntime(1062):11 ...更多
05-19 14:22:40.248:I / dalvikvm(1062):主题ID = 3:反应信号3
05-19 14:22:40.283:I / dalvikvm(1062):写的堆栈跟踪到/data/anr/traces.txt
05-19 14:22:40.608:I / dalvikvm(1062):主题ID = 3:反应信号3
05-19 14:22:40.698:I / dalvikvm(1062):写的堆栈跟踪到/data/anr/traces.txt
05-19 14:22:42.078:I /过程(1062):发送信号。 PID:1062 SIG:9
 

解决方案

简单地说,

,是你的错上的网址,只要改正。

www.google.com您有4个W公司在你的网址...

和添加使用,权限<使用-权限的Andr​​oid:名称=android.permission.INTERNET对>  在你的Andr​​oid应用程序的清单文件。

这是正确的。

地址= InetAddress.getByName(www.google.com);

编辑:使用的AsyncTask 进行网络操作。

 字符串NETADDRESS = NULL;
 尝试
  {
   。NETADDRESS =新NetTask()执行(www.google.com)获得();
  }
  赶上(例外E1)
   {
    e1.printStackTrace();
   }
 

和这个人是 NetTask 类..

 公共类NetTask扩展的AsyncTask<字符串,整数,字符串>
    {
        @覆盖
        保护字符串doInBackground(字符串... PARAMS)
        {
            InetAddress类地址= NULL;
            尝试
            {
                ADDR = InetAddress.getByName(PARAMS [0]);
            }

            赶上(UnknownHostException异常E)
            {
                            e.printStackTrace();
            }
            返回addr.getHostAddress();
        }
    }
 

How to print the IP address of a website in android ? I can run inetaddress and print it by using system.out.println() in netbean. Below is my sample coding.

public String getHostAddress () {
        InetAddress addr=null;
        try {
            addr= InetAddress.getByName("www.google.com");
        }

        catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        return addr.getHostAddress();
    }

It always show Unfortunately, your program has stopped. May know is there any way to get the IP address of visited website in android ?

05-19 14:22:39.008: I/dalvikvm(1062): threadid=3: reacting to signal 3
05-19 14:22:39.049: I/dalvikvm(1062): Wrote stack traces to '/data/anr/traces.txt'
05-19 14:22:39.688: I/dalvikvm(1062): threadid=3: reacting to signal 3
05-19 14:22:39.828: I/dalvikvm(1062): Wrote stack traces to '/data/anr/traces.txt'
05-19 14:22:39.929: D/AndroidRuntime(1062): Shutting down VM
05-19 14:22:39.948: W/dalvikvm(1062): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-19 14:22:40.039: E/AndroidRuntime(1062): FATAL EXCEPTION: main
05-19 14:22:40.039: E/AndroidRuntime(1062): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.destinationurl/com.android.destinationurl.DestinationURL}: android.os.NetworkOnMainThreadException
05-19 14:22:40.039: E/AndroidRuntime(1062):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at android.os.Looper.loop(Looper.java:137)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at java.lang.reflect.Method.invokeNative(Native Method)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at java.lang.reflect.Method.invoke(Method.java:511)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at dalvik.system.NativeStart.main(Native Method)
05-19 14:22:40.039: E/AndroidRuntime(1062): Caused by: android.os.NetworkOnMainThreadException
05-19 14:22:40.039: E/AndroidRuntime(1062):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at java.net.InetAddress.getByName(InetAddress.java:295)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at com.android.destinationurl.DestinationURL.getHostAddress(DestinationURL.java:57)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at com.android.destinationurl.DestinationURL.onCreate(DestinationURL.java:40)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at android.app.Activity.performCreate(Activity.java:4465)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-19 14:22:40.039: E/AndroidRuntime(1062):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-19 14:22:40.039: E/AndroidRuntime(1062):     ... 11 more
05-19 14:22:40.248: I/dalvikvm(1062): threadid=3: reacting to signal 3
05-19 14:22:40.283: I/dalvikvm(1062): Wrote stack traces to '/data/anr/traces.txt'
05-19 14:22:40.608: I/dalvikvm(1062): threadid=3: reacting to signal 3
05-19 14:22:40.698: I/dalvikvm(1062): Wrote stack traces to '/data/anr/traces.txt'
05-19 14:22:42.078: I/Process(1062): Sending signal. PID: 1062 SIG: 9

解决方案

Simply,

Its your mistake on URL, Just correct it..

"www.google.com" you have 4 w's in your URL...

And add Use-Permission <uses-permission android:name="android.permission.INTERNET"> in manifest file of your android application..

This one is correct..

addr = InetAddress.getByName("www.google.com");

EDIT: Use AsyncTask for Network Operation

String netAddress = null;
 try
  {
   netAddress = new NetTask().execute("www.google.com").get();
  }
  catch (Exception e1)
   {
    e1.printStackTrace();
   }

And this one is NetTask class..

public class NetTask extends AsyncTask<String, Integer, String>
    {
        @Override
        protected String doInBackground(String... params)
        {
            InetAddress addr = null;
            try
            {
                addr = InetAddress.getByName(params[0]);
            }

            catch (UnknownHostException e)
            {
                            e.printStackTrace();
            }
            return addr.getHostAddress();
        }
    }

这篇关于InetAddress类在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆