【问题标题】:I get "Background sticky concurrent mark sweep G" in logs and then nothing happens我在日志中得到“背景粘性并发标记扫描 G”,然后什么也没有发生
【发布时间】:2018-11-21 22:39:11
【问题描述】:

我正在尝试连接到 FTP 服务器并在从服务器中提取数据后在我的 android 应用程序上显示数据,但我无法弄清楚。我正面临上述问题。 FTPDownloader 类在 MainActivity 类中,我称之为 新的 FTPDownloader().execute();。在 MainActivity 类中按下按钮后,我调用 FTPDownloader() 的 doInBackground()。但是没有任何事情发生,就好像 doInBackground() 方法从未运行过一样。该应用程序虽然没有冻结,但也没有任何反应。提前感谢,任何帮助表示赞赏。

   private class FTPDownloader extends AsyncTask<Void, Void, Void> {

        FTPClient ftp = null;
        InputStream in;

        public FTPDownloader() {
            ftp = null;
        }


        public void disconnect() {
            if (this.ftp.isConnected()) {
                try {
                    this.ftp.logout();
                    this.ftp.disconnect();
                } catch (IOException f) {
                    // do nothing as file is already downloaded from FTP server
                }
            }
        }

        @Override
        protected Void doInBackground(Void... voids) {
            try {

                ftp = new FTPClient();
                ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
                int reply;
                ftp.connect("12.123.12.123");
                reply = ftp.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply)) {
                    ftp.disconnect();
                    throw new Exception("Exception in connecting to FTP Server");
                }
                ftp.login("user1234","1234" );
                ftp.setFileType(FTP.BINARY_FILE_TYPE);
                ftp.enterLocalPassiveMode();
                in = ftp.retrieveFileStream("filePath");


            } catch (Exception e) {
                e.printStackTrace();
                Log.i("FTP", "Error Occurred.");

            }
             try {

                int data = in.read();

                while (data != -1) {
                    String s = "";
                    char ch = (char) data;
                    if (ch != ',') {
                        s = s + ch;
                    } else {
                        s = s + " ";
                        gblprpd.add(s);
                        data = in.read();
                    }
                }
                in.close();

                for (int i = 0; i < gblprpd.size(); ++i) {
                    String s = "";
                    for (int j = 0; j < 9; ++j) {
                        ++i;
                        s = s + gblprpd.get(i);
                    }

                    dta.add(s);
                }
            } catch (IOException e) {
                e.printStackTrace();
                Log.i("FTP", "Error Occurred");

            }

            disconnect();
            return null;
        }

        @Override
        protected void onPreExecute()
        {

        }

        @Override
        protected void onPostExecute(Void cd) {


                    TextView output = (TextView) findViewById(R.id.output);
                    output.setText("Data Retrieved:");
                    for (int k = 0; k < dta.size(); ++k) {
                        roes.get(k + 1).setText(dta.get(k));
                    }
                    for (int k = dta.size() + 1; k < 16; ++k)
                        roes.get(k).setVisibility(View.GONE);
                    ScrollView sv = (ScrollView) findViewById(R.id.sv);
                    sv.setVisibility(View.VISIBLE);
                    HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv);
                    hsv.setVisibility(View.VISIBLE);


    }
}


}

日志是:

06-12 21:08:30.797 23404-23469/com.example.quickstart I/System.out: 220 Microsoft FTP Service
06-12 21:08:30.800 23404-23469/com.example.quickstart I/System.out: USER user1234
06-12 21:08:30.861 23404-23469/com.example.quickstart I/System.out: 331 Password required
06-12 21:08:30.862 23404-23469/com.example.quickstart I/System.out: PASS 1234
06-12 21:08:30.936 23404-23469/com.example.quickstart I/System.out: 230 User logged in.
06-12 21:08:30.938 23404-23469/com.example.quickstart I/System.out: TYPE I
06-12 21:08:31.018 23404-23469/com.example.quickstart I/System.out: 200 Type set to I.
06-12 21:08:31.019 23404-23469/com.example.quickstart I/System.out: PASV
06-12 21:08:31.082 23404-23469/com.example.quickstart I/System.out: 227 Entering Passive Mode (14,141,70,165,66,149).
06-12 21:08:31.145 23404-23469/com.example.quickstart I/System.out: RETR File\Path
06-12 21:08:31.213 23404-23469/com.example.quickstart I/System.out: 125 Data connection already open; Transfer starting.
06-12 21:08:46.814 23404-23415/com.example.quickstart I/art: Background sticky concurrent mark sweep GC freed 910864(25MB) AllocSpace objects, 0(0B) LOS objects, 27% free, 29MB/40MB, paused 1.234ms total 106.681ms
06-12 21:08:46.976 23404-23415/com.example.quickstart I/art: Background sticky concurrent mark sweep GC freed 926388(25MB) AllocSpace objects, 0(0B) LOS objects, 28% free, 29MB/40MB, paused 1.118ms total 104.876ms

请帮助,任何帮助表示赞赏。提前致谢

【问题讨论】:

    标签: java android ftp apache-commons-net


    【解决方案1】:

    您正在从onPostExecute() 中的 FTP 连接读取字节。 onPostExecute() 在主应用程序线程上运行。因此,您正在主应用程序线程上执行网络 I/O。

    将使用 FTP 连接的代码全部移至doInBackground()。使用onPostExecute()中下载的数据。

    另请注意,onPostExecute() 中不需要runOnUiThread(),因为onPostExecute() 已经在主应用程序(“UI”)线程上运行。

    【讨论】:

    • 我改变了上面的代码,但现在我得到了 "java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()" ,错误
    • @KushagrTyagi:如果您仍然有问题,请使用您的新代码和新堆栈跟踪发布一个单独的堆栈溢出问题。
    • while 循环中出现错误,导致它成为无限循环。我解决了这个问题,它工作得很好。感谢大家的时间和帮助。
    猜你喜欢
    • 2017-07-11
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2015-07-08
    • 1970-01-01
    相关资源
    最近更新 更多