网址只是文本文件,例如www.example.com/example.txt,所以我需要做的是从网站上获取整个文本。文本可以长达1MB。我不知道为什么我的代码不起作用。当我运行代码时,进度对话框显示并消失,但文本未加载,我没有收到任何错误。
以下是我的代码的一部分
public class Story extends Activity {
Elements elem = null;
Document document;
String url = "http://www.example.com/example.txt";
ProgressDialog progressDialog;
private class Title extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(Story.this);
progressDialog.setMessage("Loading");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
document = Jsoup.connect(url).get();
} catch (IOException e) {
e.printStackTrace();
}
elem = document.select("*");
return null;
}
@Override
protected void onPostExecute(Void result) {
for (Element element : elem){
String valueofelement = element.text();
story.add(valueofelement); //story is an a
progressDialog.dismiss();
}
}
}
我用new Title().execute();
这是LogCat
11-29 17:13:07.563: D/KenBurnsView(15380): swapImage active=0
11-29 17:13:07.563: D/KenBurnsView(15380): new active=1
11-29 17:13:07.568: D/KenBurnsView(15380): starting Ken Burns animation android.view.ViewPropertyAnimator@42d5bfb8
11-29 17:13:18.103: D/AbsListView(18123): Get MotionRecognitionManager
11-29 17:13:18.178: D/dalvikvm(18123): GC_FOR_ALLOC freed 51K, 9% free 12366K/13447K, paused 22ms, total 22ms
11-29 17:13:18.183: I/dalvikvm-heap(18123): Grow heap (frag case) to 13.915MB for 971248-byte allocation
11-29 17:13:18.208: D/dalvikvm(18123): GC_CONCURRENT freed 2K, 8% free 13312K/14407K, paused 12ms+1ms, total 24ms
11-29 17:13:18.233: D/dalvikvm(18123): GC_FOR_ALLOC freed <1K, 8% free 13313K/14407K, paused 15ms, total 15ms
11-29 17:13:18.243: I/dalvikvm-heap(18123): Grow heap (frag case) to 15.540MB for 1705616-byte allocation
11-29 17:13:18.268: D/dalvikvm(18123): GC_CONCURRENT freed <1K, 8% free 14978K/16135K, paused 19ms+1ms, total 28ms
11-29 17:13:18.608: D/libEGL(18123): loaded /system/lib/egl/libEGL_mali.so
11-29 17:13:18.638: D/libEGL(18123): loaded /system/lib/egl/libGLESv1_CM_mali.so
11-29 17:13:18.643: D/libEGL(18123): loaded /system/lib/egl/libGLESv2_mali.so
11-29 17:13:18.663: D/(18123): Device driver API match
11-29 17:13:18.663: D/(18123): Device driver API version: 10
11-29 17:13:18.663: D/(18123): User space API version: 10
11-29 17:13:18.663: D/(18123): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
11-29 17:13:18.698: D/OpenGLRenderer(18123): Enabling debug mode 0
11-29 17:13:18.953: D/KenBurnsView(18123): swapImage active=-1
11-29 17:13:19.003: D/KenBurnsView(18123): starting Ken Burns animation android.view.ViewPropertyAnimator@42d5e370
11-29 17:13:19.503: D/dalvikvm(18123): GC_CONCURRENT freed 580K, 10% free 15325K/16903K, paused 88ms+1ms, total 115ms
11-29 17:13:22.583: W/IInputConnectionWrapper(18123): getSelectedText on inactive InputConnection
11-29 17:13:22.588: W/IInputConnectionWrapper(18123): setComposingText on inactive InputConnection
11-29 17:13:22.623: W/IInputConnectionWrapper(18123): getExtractedText on inactive InputConnection
11-29 17:13:28.203: D/KenBurnsView(18123): swapImage active=1
11-29 17:13:28.203: D/KenBurnsView(18123): new active=0
11-29 17:13:28.208: D/KenBurnsView(18123): starting Ken Burns animation android.view.ViewPropertyAnimator@42e55930
11-29 17:13:37.433: D/KenBurnsView(18123): swapImage active=0
11-29 17:13:37.433: D/KenBurnsView(18123): new active=1
11-29 17:13:37.788: D/KenBurnsView(18123): starting Ken Burns animation android.view.ViewPropertyAnimator@42d5e370
答案 0 :(得分:0)
您无需使用Elements即可完成此操作。简单地使用doc.body().ownText()
(一个字符串)来从页面获取干净的,无HTML的文本要简单得多,特别是因为您只是从文本文件加载。
将elem = document.select("*");
替换为strText = document.body().ownText()
。你必须定义一个新的String。
最后,请替换以下内容:
for (Element element : elem){
String valueofelement = element.text();
story.add(valueofelement);
progressDialog.dismiss();
}
有了这个:
story.add(strText);
progressDialog.dismiss();
您的代码现在应该按预期工作。