Json data store in SQLite in Android -
i trying store data in sqlite data base. can not that. give error. code , please me fix this.
public class androidjsonparsingactivity extends listactivity { // 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); // hashmap listview arraylist<hashmap<string, string>> contactlist = new arraylist<hashmap<string, string>>(); // creating json parser instance jsonparser jparser = new jsonparser(); // getting json string url jsonobject json = jparser.getjsonfromurl(url); try { // getting array of contacts contacts = json.getjsonarray(tag_contacts); // looping through contacts (int = 0; < contacts.length(); i++) { jsonobject c = contacts.getjsonobject(i); // storing each json item in variable string id = c.getstring(tag_id); string name = c.getstring(tag_name); string email = c.getstring(tag_email); string address = c.getstring(tag_address); string gender = c.getstring(tag_gender); // phone number agin json object jsonobject phone = c.getjsonobject(tag_phone); string mobile = phone.getstring(tag_phone_mobile); string home = phone.getstring(tag_phone_home); string office = phone.getstring(tag_phone_office); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(tag_id, id); map.put(tag_name, name); map.put(tag_email, email); map.put(tag_phone_mobile, mobile); // adding hashlist arraylist contactlist.add(map); // database edits button add = (button) findviewbyid(r.id.button1); add.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub string sqlname = name; string sqlemail = email; string sqlmobile = mobile; sqlhandler entry = new sqlhandler( androidjsonparsingactivity.this); entry.open(); entry.createentry(sqlname, sqlemail, sqlmobile); entry.close(); } }); } } catch (jsonexception e) { e.printstacktrace(); } /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter(this, contactlist, r.layout.list_item, new string[] { tag_name, tag_email, tag_phone_mobile }, new int[] { r.id.name, r.id.email, r.id.mobile }); setlistadapter(adapter); // selecting single listview item listview lv = getlistview(); // launching new screen on selecting single listitem lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string name = ((textview) view.findviewbyid(r.id.name)) .gettext().tostring(); string cost = ((textview) view.findviewbyid(r.id.email)) .gettext().tostring(); string description = ((textview) view.findviewbyid(r.id.mobile)) .gettext().tostring(); // starting new intent intent in = new intent(getapplicationcontext(), singlemenuitemactivity.class); in.putextra(tag_name, name); in.putextra(tag_email, cost); in.putextra(tag_phone_mobile, description); startactivity(in); } }); }
}
here sqlopenhelper class
public class sqlhandler { public static final string key_rowid = "_id"; public static final string key_name = "name"; public static final string key_email = "email"; public static final string key_mobile = "mobile"; private static final string database_name = "details"; private static final string database_table = "topdetails"; private static final int database_version = 1; private dbhelper ourhelper; private final context ourcontext; private sqlitedatabase ourdatabase; private static class dbhelper extends sqliteopenhelper { public dbhelper(context context) { super(context, database_name, null, database_version); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql("create table " + database_table + " (" + key_rowid + " integer primary key autoincrement, " + key_name + " text not null, " + key_email + " text not null, " + key_mobile + " text not null);"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop table if exists " + database_table); oncreate(db); } } public sqlhandler(context c) { ourcontext = c; } public sqlhandler open() throws sqlexception { ourhelper = new dbhelper(ourcontext); ourdatabase = ourhelper.getwritabledatabase(); return this; } public void close() { // todo auto-generated method stub ourhelper.close(); } public void createentry(string name, string email, string mobile) { // todo auto-generated method stub contentvalues cv = new contentvalues(); cv.put(key_name, name); cv.put(key_email, email); cv.put(key_mobile, mobile); ourdatabase.insert(database_table, null, cv); } }
and json parser
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } public jsonobject getjsonfromurl(string url) { // making http request try { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(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, "iso-8859-1"), 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; } }
also single menu item activity
public class singlemenuitemactivity extends activity { // json node keys private static final string tag_name = "name"; private static final string tag_email = "email"; private static final string tag_phone_mobile = "mobile"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.single_list_item); // getting intent data intent in = getintent(); // json values previous intent string name = in.getstringextra(tag_name); string cost = in.getstringextra(tag_email); string description = in.getstringextra(tag_phone_mobile); // displaying values on screen textview lblname = (textview) findviewbyid(r.id.name_label); textview lblcost = (textview) findviewbyid(r.id.email_label); textview lbldesc = (textview) findviewbyid(r.id.mobile_label); lblname.settext(name); lblcost.settext(cost); lbldesc.settext(description); } }
thanks. please me correct this.
you have initialize class helper in main activity.
Comments
Post a Comment