我正在尝试检查是否可以使用 android 应用程序访问 Web 服务。
在我的按钮点击监听器中是:
try {
InetAddress address = InetAddress.getByName(result); // user input is 'result' (a URL) LINE 73
boolean b = address.isReachable(3000);
String str = String.valueOf(b); // turning the value of the boolean into string
pingResult.setText(str); // value displays as true or false
}
catch (UnknownHostException e) {} // will fill with helpful message later
catch (IOException e) {}
Logcat 显示:
05-10 15:21:21.390: E/AndroidRuntime(11653): FATAL EXCEPTION: main
05-10 15:21:21.390: E/AndroidRuntime(11653): Process: com.example.c3po, PID: 11653
05-10 15:21:21.390: E/AndroidRuntime(11653): android.os.NetworkOnMainThreadException
05-10 15:21:21.390: E/AndroidRuntime(11653): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
05-10 15:21:21.390: E/AndroidRuntime(11653): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-10 15:21:21.390: E/AndroidRuntime(11653): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-10 15:21:21.390: E/AndroidRuntime(11653): at java.net.InetAddress.getByName(InetAddress.java:289)
05-10 15:21:21.390: E/AndroidRuntime(11653): at com.example.c3po.MainActivity$2.onClick(MainActivity.java:73)
还有一些,第 73 行已被评论。
有什么想法我哪里出错了吗?
谢谢