从网站查看大文本文件的最佳方法是什么?

时间:2013-11-26 20:01:22

标签: android android-listview android-asynctask android-webview

我尝试了webview.loadurl("www.blahblah.com/blah.txt")并在几秒钟后崩溃了。我还能做什么?

我只想尽可能快地和稳定地从网上查看这些.txt文件。

我在研究时遇到了asynctask。它是什么,它可以帮助我吗?

如果我想将这些文本文件显示为listview项目,如何将它们设为字符串?

1 个答案:

答案 0 :(得分:0)

Asynctask是一个新线程的包装器,允许您执行下载等网络任务。如果您尝试在UI线程上下载,则会发生崩溃。

对于文本文件,首先将其下载到光盘,然后将其读入内存并显示

StringBuilder txtString = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String textLine;

    while ((textLine = br.readLine()) != null) {
        txtString.append(line);
        txtString.append('\n');
    }
}
catch (IOException e) {
//error stuff
}