java - Android asynctask json parsing getting error android.os.NetworkOnMainThreadException -
i'm new android, , try parse json url using such class:
package com.***.exchanger; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonexception; import org.json.jsonobject; import android.util.log; public class jsonparser { static inputstream = null; static jsonobject jobj; static string json = ""; // constructor public jsonparser() { } public jsonobject getjsonfromurl(string url) { // making http request try { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httpget httppost = new httpget(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "utf-8"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
and when parsing without asynctask - good, json results, etc...
but code such:
public class exchangerlistactivity extends listactivity { private static string url; private static final string tag_banks = "bank"; private static final string tag_id = "id"; private static final string tag_name = "name"; private static final string tag_exc_count = "exch_count"; private static final string tag_central_office_address = "central_office_address"; jsonarray banks = null; jsonobject json; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_exchanger_list); url = "http://192.168.1.4:3000/banks.json"; jsonparser jparser = new jsonparser(); jsonobject json = jparser.getjsonfromurl(url); listviewloadertask listviewloadertask = new listviewloadertask(); listviewloadertask.execute(json); } private class listviewloadertask extends asynctask<jsonobject, void, listadapter>{ jsonobject jobject; /** doing parsing of xml data in non-ui thread */ @override protected listadapter doinbackground(jsonobject... strjson) { url = "http://192.168.1.4:3000/banks.json"; arraylist<hashmap<string, string>> contactlist = new arraylist<hashmap<string, string>>(); jsonparser jparser = new jsonparser(); jsonobject json = jparser.getjsonfromurl(url); try { banks = json.getjsonarray(tag_banks); for(int = 0; < banks.length(); i++){ jsonobject c = banks.getjsonobject(i); string id = c.getstring(tag_id); string name = c.getstring(tag_name); string central_office_address = c.getstring(tag_central_office_address); string exchangers_list_count = c.getstring(tag_exc_count); hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_id, id); map.put(tag_name, name); map.put(tag_central_office_address, central_office_address); map.put(tag_exc_count, exchangers_list_count); contactlist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } final listadapter adapter = new simpleadapter(exchangerlistactivity.this, contactlist, r.layout.bank_list, new string[] { tag_name, tag_central_office_address, tag_exc_count }, new int[] { r.id.bank_name, r.id.central_office_address, r.id.exchangers_list_count}); return adapter; } /** invoked android system on "doinbackground" executed */ /** executed in ui thread */ @override protected void onpostexecute(listadapter adapter) { /** getting reference listview of main.xml layout file */ listview listview = exchangerlistactivity.this.getlistview(); /** setting adapter containing country list listview */ listview.setadapter(adapter); //setlistadapter(adapter); /* lv.setonitemclicklistener(new adapterview.onitemclicklistener() { */ @suppresswarnings("unchecked") @override public void onitemclick(adapterview<?> parent, android.view.view view, int position, long id) { intent in = new intent(getapplicationcontext(), bankexchangerslistactivity.class); string = ((map<string, string>) adapter.getitem(position)).get(tag_id); in.putextra("bank_id", a); startactivity(in); } });
*/ } }
@override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.exchanger_list, menu); return true; } }
and errors:
05-02 20:16:21.749: e/androidruntime(4057): fatal exception: main 05-02 20:16:21.749: e/androidruntime(4057): java.lang.runtimeexception: unable start activity componentinfo{com.pavel.exchanger/com.pavel.exchanger.exchangerlistactivity}: android.os.networkonmainthreadexception 05-02 20:16:21.749: e/androidruntime(4057): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 05-02 20:16:21.749: e/androidruntime(4057): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 05-02 20:16:21.749: e/androidruntime(4057): @ android.app.activitythread.access$600(activitythread.java:141) 05-02 20:16:21.749: e/androidruntime(4057): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 05-02 20:16:21.749: e/androidruntime(4057): @ android.os.handler.dispatchmessage(handler.java:99) 05-02 20:16:21.749: e/androidruntime(4057): @ android.os.looper.loop(looper.java:137) 05-02 20:16:21.749: e/androidruntime(4057): @ android.app.activitythread.main(activitythread.java:5041) 05-02 20:16:21.749: e/androidruntime(4057): @ java.lang.reflect.method.invokenative(native method) 05-02 20:16:21.749: e/androidruntime(4057): @ java.lang.reflect.method.invoke(method.java:511) 05-02 20:16:21.749: e/androidruntime(4057): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-02 20:16:21.749: e/androidruntime(4057): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-02 20:16:21.749: e/androidruntime(4057): @ dalvik.system.nativestart.main(native method) 05-02 20:16:21.749: e/androidruntime(4057): caused by: android.os.networkonmainthreadexception 05-02 20:16:21.749: e/androidruntime(4057): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1117) 05-02 20:16:21.749: e/androidruntime(4057): @ libcore.io.blockguardos.connect(blockguardos.java:84) 05-02 20:16:21.749: e/androidruntime(4057): @ libcore.io.iobridge.connecterrno(iobridge.java:127) 05-02 20:16:21.749: e/androidruntime(4057): @ libcore.io.iobridge.connect(iobridge.java:112) 05-02 20:16:21.749: e/androidruntime(4057): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) 05-02 20:16:21.749: e/androidruntime(4057): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:459) 05-02 20:16:21.749: e/androidruntime(4057): @ java.net.socket.connect(socket.java:842) 05-02 20:16:21.749: e/androidruntime(4057): @ org.apache.http.conn.scheme.plainsocketfactory.connectsocket(plainsocketfactory.java:119) 05-02 20:16:21.749: e/androidruntime(4057): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:144) 05-02 20:16:21.749: e/androidruntime(4057): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 05-02 20:16:21.749: e/androidruntime(4057): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 05-02 20:16:21.749: e/androidruntime(4057): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 05-02 20:16:21.749: e/androidruntime(4057): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 05-02 20:16:21.749: e/androidruntime(4057): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 05-02 20:16:21.749: e/androidruntime(4057): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 05-02 20:16:21.749: e/androidruntime(4057): @ com.pavel.exchanger.jsonparser.getjsonfromurl(jsonparser.java:39) 05-02 20:16:21.749: e/androidruntime(4057): @ com.pavel.exchanger.exchangerlistactivity.oncreate(exchangerlistactivity.java:45) 05-02 20:16:21.749: e/androidruntime(4057): @ android.app.activity.performcreate(activity.java:5104) 05-02 20:16:21.749: e/androidruntime(4057): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 05-02 20:16:21.749: e/androidruntime(4057): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 05-02 20:16:21.749: e/androidruntime(4057): ... 11 more
what i'm doing wrong? how run activity json parsing , displaying?
what i'm doing wrong?
you calling getjsonfromurl()
oncreate()
of activity (see line 45 of exchangerlistactivity.java
). in addition getjsonfromurl()
in asynctask
. presumably forgot delete case calling in oncreate()
.
Comments
Post a Comment