[英]Trying to get text from an url with AsyncTask and Jsoup but the text doesn't show
这些URL只是像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();
称呼它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
您无需使用Elements即可完成此任务。 简单地使用doc.body().ownText()
(一个String)来从页面中获取干净的,无HTML的文本要简单得多,尤其是因为您仅从文本文件中加载。
替换elem = document.select("*");
与strText = document.body().ownText()
。 当然,您必须定义一个新的字符串。
最后,替换以下内容:
for (Element element : elem){
String valueofelement = element.text();
story.add(valueofelement);
progressDialog.dismiss();
}
有了这个:
story.add(strText);
progressDialog.dismiss();
您的代码现在应该可以正常工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.