English 中文(简体)
• 如何在安妮特·阿诺特网站印刷IP地址
原标题:How to print the IP address of a website in Android with InetAddress
  • 时间:2012-05-19 06:12:27
  •  标签:
  • android

如何在安伯网站印刷IP地址? 我可以使用<代码>InetAddress,并在网上使用<编码>systemout.println()。 以下是我的样本编码:

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 shows "Unfortunately, your program has stopped."
May I know is there any way to get the IP address of the 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
最佳回答

简而言之,

阁下对URL的错误是正确的。

www.google.com>, 您的URL.

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

这是正确的。

http://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();
   }

其中一个是NetTask

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();
        }
    }
问题回答

你错误地用wwww.google.com进入URL。

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

After correction it worked.

确保你获得所需的一切许可。 或许你的申请因没有适当的许可而停止。 如果你在XML表中获得互联网接入许可,进行检查。

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

这是因为你正在把网络引向主线。

String address = "";
AsyncTask.execute(new Runnable() {
    @Override
    public void run() {
        address = InetAddress.getByName("www.google.com");
    }
});




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签