如何平起Java的Andr​​oid的外部IP [英] How to Ping External IP from Java Android

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

问题描述

我开发一个Ping应用程序为Android 2.2。

我尽我的code和它的作品,但只有在当地的IP地址,这是我的问题,我想这样做的ping到外部服务器了。

下面是我的code:

 私人OnClickListener milistener =新OnClickListener(){
    公共无效的onClick(视图v){
        TextView的信息=(TextView中)findViewById(R.id.info);
        编辑的EditText =(EditText上)findViewById(R.id.edit);
        可编辑的主机= edit.getText();
        InetAddress类的;
        在= NULL;
        // Definimos拉IP德拉CUAL haremos埃尔平
        尝试 {
            在= InetAddress.getByName(host.toString());
        }赶上(UnknownHostException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
        // Definimos联合国的Tiempo连接埃尔CUAL公顷德应答
        尝试 {
            如果(in.isReachable(5000)){
                info.setText(Responde行);
            } 其他 {
                info.setText(无responde:超时);
            }
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            info.setText(e.toString());
        }
    }
};
 

  

平127.0.0.1 - >确定
  平8.8.8.8(谷歌DNS) - >超时

我把以下行清单XML太:

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对>< /使用-许可>
 

任何人都可以给我建议,我做错了什么?

解决方案

我试着以下code,这对我的作品。

 私人布尔的ExecuteCommand(){
        的System.out.println(的ExecuteCommand);
        运行时运行时间=调用Runtime.getRuntime();
        尝试
        {
            流程mIpAddrProcess =的Runtime.exec(/系统/斌/平-c 1 8.8.8.8);
            INT mExitValue = mIpAddrProcess.waitFor();
            的System.out.println(mExitValue+ mExitValue);
            如果(mExitValue == 0){
                返回true;
            }其他{
                返回false;
            }
        }
        赶上(InterruptedException的忽略)
        {
            ignore.printStackTrace();
            的System.out.println(异常:+忽略);
        }
        赶上(IOException异常E)
        {
            e.printStackTrace();
            的System.out.println(异常:+ E);
        }
        返回false;
    }
 

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的Andr​​oid的外部IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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