ping到android中的外部服务器

时间:2015-09-15 06:51:35

标签: android

我正在编写android代码来ping服务器。但问题是我只能ping到localhost服务器。当我尝试ping外部服务器或像google.com这样的主机时,代码无效。

package com.ping;

import java.io.*;
import android.app.*;
import android.os.Bundle;
import android.widget.*;

public class MainActivity extends Activity 
{


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

    text= (TextView)findViewById(R.id.pingview);
    start();

}

protected boolean start()
{
    try
    {
        Runtime runtime1 = Runtime.getRuntime();
        Process proc = runtime1.exec("ping -c 1 202.51.67.57"); 
        BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String input;

        while((input=reader.readLine())!=null)
        {
            text.setText(input+"\n");
        }

    }

    catch (IOException e) 
    {
        e.printStackTrace();
        System.out.println(" Exception:"+e);
    }
    return false;
}

}

0 个答案:

没有答案
相关问题