java - Error: android.os.NetworkOnMainThreadException -
this question has answer here:
i'm making android app. i've made activity loads information web , use information set listview. if run app works fine, if open activity (which described above) gives 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 listview resource. mainlistview = (listview) findviewbyid( r.id.listview2); string file_name = "http://3gaatleren.16mb.com/appagenda/agenda.html"; // create , populate 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 planet list. arrayadapter<string> listadapter; listadapter = new arrayadapter<string>(agenda.this, r.layout.simplerow, huiswerklist); // set arrayadapter 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 menu; adds items action bar if present. getmenuinflater().inflate(r.menu.agenda, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
java file define 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; (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 tell me what's problem?
ps: english not first language, don't know if it's correct english
basically ui thread part of app processes what's shown on screen , let's user use it. if things don't respond when user tries @ best user thinks app slow , @ worst user thinks app has crashed , android bad, don't want.
to around can takes long time or lot of processing power on thread. easiest way asynctask.
new asynctask<void, void, void>() { public void doinbackground(void... params,) { //do loading here return null; } public void onpostexecute(void result) { //update ui here when loading finishes } }.execute();
the above absolute bare bones should solve problem
Comments
Post a Comment