用于ping的Android应用无效

时间:2016-05-24 15:48:25

标签: android networking

我尝试在Android上编写简单的ping应用程序,但它不起作用。当我尝试ping IP地址时它停止工作。这是代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

Button pingbtn = (Button) findViewById(R.id.Ping_btn);
   final  TextView Ipinfo = (TextView) findViewById(R.id.Ping_info);
    final EditText Ipfield = (EditText) findViewById(R.id.Ip_field);
    Ipfield.setText("127.0.0.1");
    pingbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Editable host = Ipfield.getText();

            InetAddress Address = null;
            try
            {
                Address = InetAddress.getByName(host.toString());
            }
            catch (UnknownHostException e)
            {
            e.printStackTrace();
            }
               try {
                   if (Address.isReachable(5000)) {
                       Ipinfo.append("\n" + host + "- Response ok");
                   } else {
                       Ipinfo.append("\n" + host);
                   }
               }
        catch(IOException e)
            {
              Ipinfo.append("\n "+ e.toString());
            }
        }
    });

}

}

这是错误

  

致命的例外:主要   处理:com.example.reaper.simpleping,PID:15226   android.os.NetworkOnMainThreadException   在   android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)                                                                                      在libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)                                                                                      在libcore.io.IoBridge.connectErrno(IoBridge.java:154)                                                                                      在libcore.io.IoBridge.connect(IoBridge.java:122)                                                                                      在java.net.InetAddress.isReachable(InetAddress.java:761)                                                                                      在java.net.InetAddress.isReachable(InetAddress.java:715)                                                                                      在java.net.InetAddress.isReachable(InetAddress.java:685)   在   com.example.reaper.simpleping.MainActivity $ 1.onClick(MainActivity.java:43)   在android.view.View.performClick(View.java:4756)   在android.view.View $ PerformClick.run(View.java:19749)                                                                                      在android.os.Handler.handleCallback(Handler.java:739)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                      在android.os.Looper.loop(Looper.java:135)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:5221)                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                      在java.lang.reflect.Method.invoke(Method.java:372)                                                                                      在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我无法理解什么是问题,请帮我找到修复此错误的方法。 感谢注意。

2 个答案:

答案 0 :(得分:0)

当应用程序尝试在其主线程上执行网络操作时引发的异常。

仅针对Honeycomb SDK或更高版本的应用程序进行此操作。针对早期SDK版本的应用程序可以在其主要事件循环线程上进行网络连接,但是非常不鼓励这样做。请参阅文档Designing for Responsiveness

答案 1 :(得分:0)

正如@dhiku所说,它非常气馁但你可以运行这段代码来允许所有类型的线程。设置内容后,在onCreate()内运行:

    //allow all threading policies
    if(Build.VERSION.SDK_INT >9){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }