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(); }
它总是显示不幸的是,你的程序已经停止.
可能知道有没有办法在AndroID中获取被访问网站的IP地址?
05-19 14:22:39.008: I/dalvikvm(1062): threadID=3: reacting to signal 305-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 305-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 VM05-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: main05-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.networkonmainthreadException05-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.access0(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.networkonmainthreadException05-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 more05-19 14:22:40.248: I/dalvikvm(1062): threadID=3: reacting to signal 305-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 305-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”您的网址中有4个w …
并添加Use-Permission< uses-permission androID:name =“androID.permission.INTERNET”>
在您的AndroID应用程序的清单文件中..
这个是正确的..
addr = InetAddress.getByname(“www.Google.com”);
编辑:使用AsyncTask进行网络 *** 作
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(); } }总结
以上是内存溢出为你收集整理的Android中的InetAddress全部内容,希望文章能够帮你解决Android中的InetAddress所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)