I want to read a remote text file and show its content in a textview. I have written this code, but it doesn't get any information from the text file and has "force stop" error. How can I find the reason of this problem or solve it? isn't there anything wrong in my code?
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;
}
}
and my onCreate code:
DownloadFile d=new DownloadFile();
d.doInBackground();
please solve my problem!