I'm making a simply android app. Now I've made a activity where it loads information from the web and use the information to set a listview. If run the app it works fine, but if I open the activity (which I described above) he gives the error: android.os.NetworkOnMainThreadException
activity file:
package com.a3gaatleren;
import android.support.v7.app.ActionBarActivity;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.a3gaatleren.ReadFile;
public class Agenda extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agenda);
doInBackground();
}
protected void doInBackground(){
try {
ListView mainListView ;
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.listView2);
String file_name = "http://3gaatleren.16mb.com/appagenda/agenda.html";
// Create and populate a List of planet names.
ReadFile file = new ReadFile(file_name);
String[] huiswerk = file.OpenFile();
ArrayList<String> huiswerklist = new ArrayList<String>();
huiswerklist.addAll( Arrays.asList(huiswerk) );
// Create ArrayAdapter using the planet list.
ArrayAdapter<String> listAdapter;
listAdapter = new ArrayAdapter<String>(Agenda.this, R.layout.simplerow, huiswerklist);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
}catch(MalformedURLException e){
System.out.println(e);
}catch(IOException f){
System.out.println(f);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.agenda, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
java file where I define the readfile function:
package com.a3gaatleren;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Scanner;
import org.jsoup.Jsoup;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile {
private String path;
public ReadFile (String file_path){
path= file_path;
}
public String[] OpenFile() throws IOException{
URL url = new URL(path);
BufferedReader textReader = new BufferedReader(new InputStreamReader(url.openStream()));
int numberOfLines=readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i< numberOfLines; i++){
String html = textReader.readLine();
org.jsoup.nodes.Document doc;
doc = Jsoup.parse(html, "utf-8");
String text = doc.body().text();
textData[i] =text;
}
textReader.close();
return textData;
}
int readLines() throws IOException{
URL file_to_read = new URL(path);
BufferedReader bf = new BufferedReader(new InputStreamReader(file_to_read.openStream()));
int numberOfLines = 0;
String str;
while ((str = bf.readLine()) != null){
numberOfLines++;
}
bf.close();
return numberOfLines;
}
}
Can someone tell me what's the problem?
PS: English is not my first language, I don't know if it's correct English