简体   繁体   English

android ping InetAddress和editText

[英]android ping InetAddress and editText

I want to input an ip in an EditText box, then give the result in a textView via button ping ( reachable or not) 我想在EditText框中输入一个ip,然后通过按钮ping在文本视图中提供结果(是否可达)

public class MainActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
            EditText editText = (EditText) findViewById(R.id.editText1);
            TextView textView = (TextView) findViewById(R.id.textView);
             String input= editText.getText().toString();

               InetAddress ip;
                ip = InetAddress.getByName(input);
                boolean reach= ip.isReachable(5000);
                   textView.setText("real ?"+reach);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        }
    });
}   
}

Whenever i press the ping button in the real phone, it comes out of the applicaton, why? 每当我在真实电话中按下ping按钮时,它就会从应用程序中消失,为什么?

I guess you are wrong with the net method, try this code and if it is still unworkable please tell me. 我想您使用net方法错了,请尝试下面的代码,如果仍然无法使用,请告诉我。

public class MainActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
            EditText editText = (EditText) findViewById(R.id.editText1);
            final TextView textView = (TextView) findViewById(R.id.textView);
             final String input= editText.getText().toString();
             new Thread(new Runnable(){
                 InetAddress ip;
                ip = InetAddress.getByName(input);
                final boolean reach= ip.isReachable(5000);
                runOnUiThread(new Runnable() {
                    textView.setText("real ?"+reach);
                });
             }).start(); 



                   textView.setText("real ?"+reach);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        }
    });
}   
}  

Idk if you are looking for an answer to this still but the reason your app is crashing is because you are not allowed to perform a network operation eg ip = InetAddress.getByName(input); 如果您仍在寻找答案,则为Idk,但您的应用程序崩溃的原因是因为不允许执行网络操作,例如ip = InetAddress.getByName(input); in the mainactivity please create another class and let it extend AsyncTask and perform your network operations there. 在mainactivity中,请创建另一个类,并使其扩展AsyncTask并在那里执行网络操作。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM