首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android java InputStreamReader

Android java InputStreamReader
EN

Stack Overflow用户
提问于 2016-06-24 13:25:26
回答 1查看 725关注 0票数 1

我正在尝试做一个应用程序,它将允许我在android中发出命令,并给我结果。这似乎工作得很好,除了等待更多输出的命令不能正确处理之外。我尝试发出命令"su && nmap 192.168.1.1“,但我得到的输出结果是nmap已经启动。有没有人知道一种方法,不仅可以获得nmap已经启动的输出,还可以使用以下代码的修改版本获得扫描结果。

代码语言:javascript
复制
try {
        EditText inputTxt = (EditText) findViewById(R.id.input);
        String str = inputTxt.getText().toString();
        Process command = Runtime.getRuntime().exec(str);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(command.getInputStream()));
        int read;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(output);

    }
    catch (IOException e) {
        Log.e("Run_Command", Log.getStackTraceString(e));
    }
EN

回答 1

Stack Overflow用户

发布于 2016-06-25 16:09:30

我想出了代码的另一个版本。这个使用thread.sleep,没有固定大小的缓冲区。

我得到的输出仍然是nmap已经启动,而不是扫描的结果,即使扫描在最终的thread.sleep完成时已经完成。

代码语言:javascript
复制
public void command(View view)
{
    String me;
    int a = 0;
    try {
        EditText inputTxt = (EditText) findViewById(R.id.input);
        String str = inputTxt.getText().toString();
        Process command = Runtime.getRuntime().exec(str);

        BufferedReader bReader = new BufferedReader(
                new InputStreamReader(command.getInputStream(), "UTF8"));
        //char [] me = new char[40960];
        String inputLine;
        while(1 == 1){
            if(a == 5) {
                break;}

        while ((inputLine = bReader.readLine()) != null) {
            me += inputLine + "\n";
        }
            while(bReader.readLine() == null) {
                a++;
                Thread.sleep(5000);
                if (a == 5) {
                    break;
                }
            }
                    }
        bReader.close();

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(me);

    }
    catch (IOException e) {
        Log.e("Run_Command", Log.getStackTraceString(e));
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38006133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档