java - Asyntask in JSON Parsing -
this sample code, make json parsing class parse data given link.
package com.billosuch.listviewblogpost; import java.util.arraylist; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.os.bundle; import android.util.log; import android.widget.listview; public class listviewblogpost extends activity { // url make request private static string url = "http://api.androidhive.info/contacts/"; // json node names private static final string tag_contacts = "contacts"; private static final string tag_id = "id"; private static final string tag_name = "name"; private static final string tag_email = "email"; private static final string tag_address = "address"; private static final string tag_gender = "gender"; private static final string tag_phone = "phone"; private static final string tag_phone_mobile = "mobile"; private static final string tag_phone_home = "home"; private static final string tag_phone_office = "office"; // contacts jsonarray jsonarray contacts = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); arraylist<searchresults> searchresultss = new arraylist<searchresults>(); final listview lv1 = (listview) findviewbyid(r.id.listview01); jsonparser jparser = new jsonparser(); jsonobject json = jparser.getjsonfromurl(url); // looping through contacts log.d("*********osr", "b4 try"); try { contacts = json.getjsonarray(tag_contacts); (int = 0; < contacts.length(); i++) { searchresults osr = new searchresults(); jsonobject c = contacts.getjsonobject(i); osr.setid(c.getstring(tag_id)); osr.setname(c.getstring(tag_name)); osr.setemail(c.getstring(tag_email)); osr.setaddress(c.getstring(tag_address)); osr.setgender(c.getstring(tag_gender)); jsonobject phone = c.getjsonobject(tag_phone); osr.setphone_mobile(phone.getstring(tag_phone_mobile)); osr.setphone_home(phone.getstring(tag_phone_home)); osr.setphone_office(phone.getstring(tag_phone_office)); searchresultss.add(osr); log.d("*********osr", osr.getname()); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } log.d("*********osr", "after try"); lv1.setadapter(new mycustombaseadapter(this, searchresultss)); } } this code showing me warning in asyntask. want supported on ics , jellybean.
skipped 391 frames! application may doing work on main thread.
change code below
package com.billosuch.listviewblogpost; import java.util.arraylist; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.os.bundle; import android.util.log; import android.widget.listview; public class listviewblogpost extends activity { // url make request private static string url = "http://api.androidhive.info/contacts/"; // json node names private static final string tag_contacts = "contacts"; private static final string tag_id = "id"; private static final string tag_name = "name"; private static final string tag_email = "email"; private static final string tag_address = "address"; private static final string tag_gender = "gender"; private static final string tag_phone = "phone"; private static final string tag_phone_mobile = "mobile"; private static final string tag_phone_home = "home"; private static final string tag_phone_office = "office"; final listview lv1; arraylist<searchresults> searchresultss; // contacts jsonarray jsonarray contacts = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); searchresultss = new arraylist<searchresults>(); lv1 = (listview) findviewbyid(r.id.listview01); new myasync(youractivity.this).execute(); } public class myasync extends asynctask<void, integer, void> { public myasync(activity activity) { this.activity = activity; context = activity; dialog = new progressdialog(context); } /** progress dialog show user backup processing. */ private progressdialog dialog; /** application context. */ private activity activity; private context context; protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); } @override protected void doinbackground(void... params) { // todo auto-generated method stub jsonparser jparser = new jsonparser(); jsonobject json = jparser.getjsonfromurl(url); // looping through contacts log.d("*********osr", "b4 try"); try { contacts = json.getjsonarray(tag_contacts); (int = 0; < contacts.length(); i++) { searchresults osr = new searchresults(); jsonobject c = contacts.getjsonobject(i); osr.setid(c.getstring(tag_id)); osr.setname(c.getstring(tag_name)); osr.setemail(c.getstring(tag_email)); osr.setaddress(c.getstring(tag_address)); osr.setgender(c.getstring(tag_gender)); jsonobject phone = c.getjsonobject(tag_phone); osr.setphone_mobile(phone.getstring(tag_phone_mobile)); osr.setphone_home(phone.getstring(tag_phone_home)); osr.setphone_office(phone.getstring(tag_phone_office)); searchresultss.add(osr); log.d("*********osr", osr.getname()); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } log.d("*********osr", "after try"); return null; } @override protected void onpostexecute(void result) { // todo auto-generated method stub super.onpostexecute(result); lv1.setadapter(new mycustombaseadapter(this, searchresultss)); } } }
Comments
Post a Comment