我想读取远程文本文件并在文本视图中显示其内容。我已经编写了这段代码,但它没有从文本文件中获取任何信息,并且出现“强制停止”错误。我怎样才能找到这个问题的原因或解决它?我的代码没有什么问题吗?
private class DownloadFile extends AsyncTask<String, Integer, Void> {
protected void doInBackground() {
try {
URL url = new URL("http://host/f.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line = null;
while ((line = in.readLine()) != null) {
//get lines
}
in.close();
lbl.setText(line);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
protected void onProgressUpdate() {
//called when the background task makes any progress
}
protected void onPreExecute() {
//called before doInBackground() is started
}
protected void onPostExecute() {
//called after doInBackground() has finished
}
@Override
protected Void doInBackground(String... arg0) {
// TODO Auto-generated method stub
return null;
}
}
和我的 onCreate 代码:
DownloadFile d=new DownloadFile();
d.doInBackground();
请解决我的问题!