【发布时间】:2013-11-28 22:14:10
【问题描述】:
网址只是像 www.example.com/example.txt 这样的文本文件,所以我需要做的是从网站获取整个文本。文本可以很长,最长可达 1MB。我不知道我应该如何修改我的代码来做到这一点。
这是我的代码
private class Title extends AsyncTask<Void, Void, Void> {
String text;
@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 document = Jsoup.connect(url).get();
text = document.text(); //I made this part up. Definitely WRONG
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
story.add(text); //story is an array
progressDialog.dismiss();
}
}
我用new Title().execute();调用它
此代码不起作用,我无法获取文本。什么都没有发生。
【问题讨论】:
-
只是想知道,如果您正在阅读文本文件,为什么要使用 Jsoup 和 Document 类?尝试这个? stackoverflow.com/questions/2922210/…
-
doInBackground 方法返回 null 那么你将在 onPostExecute 上使用什么?我认为 doInBackground 必须返回 sth 才能使用 onPostExecute。
-
请注意,关于确保流已关闭,并且没有为请求设置超时,答案似乎并不完整,所以买家要小心。
标签: java android android-asynctask html-parsing jsoup