如何从 Java Android ping 外部 IP [英] How to Ping External IP from Java Android

查看:26
本文介绍了如何从 Java Android ping 外部 IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Android 2.2 开发 Ping 应用程序.

我尝试了我的代码,它可以工作,但只能在本地 IP 中运行,这是我的问题,我也想对外部服务器执行 ping 操作.

这是我的代码:

 private OnClickListener milistener = new OnClickListener() {public void onClick(View v) {TextView info = (TextView) findViewById(R.id.info);EditText 编辑 = (EditText) findViewById(R.id.edit);可编辑主机 = edit.getText();Inet地址在;在 = 空;//Definimos la ip de la cual haremos el ping尝试 {in = InetAddress.getByName(host.toString());} catch (UnknownHostException e) {//TODO 自动生成的 catch 块e.printStackTrace();}//Definimos un tiempo en el cual ha de Responder尝试 {如果(in.isReachable(5000)){info.setText("回复OK");} 别的 {info.setText("无响应:超时");}} catch (IOException e) {//TODO 自动生成的 catch 块info.setText(e.toString());}}};

<块引用>

Ping 127.0.0.1 -> 好的
Ping 8.8.8.8 (Google DNS) -> 超时

我也在 Manifest XML 中添加了以下行:

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

谁能告诉我哪里做错了?

解决方案

我尝试了以下代码,这对我有用.

私有布尔值executeCommand(){System.out.println("执行命令");运行时运行时 = Runtime.getRuntime();尝试{Process mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");int mExitValue = mIpAddrProcess.waitFor();System.out.println(" mExitValue "+mExitValue);如果(mExitValue==0){返回真;}别的{返回假;}}catch (InterruptedException 忽略){忽略.printStackTrace();System.out.println(" 异常:"+忽略);}捕获(IOException e){e.printStackTrace();System.out.println("异常:"+e);}返回假;}

I am developing a Ping application for Android 2.2.

I try my code and it works, but only in local IPs, that's my problem I want to do ping to external servers too.

Here is my code:

  private OnClickListener milistener = new OnClickListener() {
    public void onClick(View v) {
        TextView info = (TextView) findViewById(R.id.info);
        EditText edit = (EditText) findViewById(R.id.edit);
        Editable host = edit.getText();
        InetAddress in;
        in = null;
        // Definimos la ip de la cual haremos el ping
        try {
            in = InetAddress.getByName(host.toString());
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Definimos un tiempo en el cual ha de responder
        try {
            if (in.isReachable(5000)) {
                info.setText("Responde OK");
            } else {
                info.setText("No responde: Time out");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            info.setText(e.toString());
        }
    }
};

Ping 127.0.0.1 -> OK
Ping 8.8.8.8 (Google DNS) -> Time Out

I put the following line at Manifest XML too:

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

Can anyone suggest me where I'm doing wrong?

解决方案

I tried following code, which works for me.

private boolean executeCommand(){
        System.out.println("executeCommand");
        Runtime runtime = Runtime.getRuntime();
        try
        {
            Process  mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
            int mExitValue = mIpAddrProcess.waitFor();
            System.out.println(" mExitValue "+mExitValue);
            if(mExitValue==0){
                return true;
            }else{
                return false;
            }
        }
        catch (InterruptedException ignore)
        {
            ignore.printStackTrace();
            System.out.println(" Exception:"+ignore);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
            System.out.println(" Exception:"+e);
        }
        return false;
    }

这篇关于如何从 Java Android ping 外部 IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆