Displaying database records in android using JAVA RESTful webservice -
i trying display database record using java restful web service. have able create login form using cannot display records on database. tried code not working @ all. when button pressed nothing happens. heres code.
driverdetails.java
class details extends activity {
textview name1; textview plate1; button btngetdata; //url json array private static string url = "http://192.168.254.108:8080/taxisafe/display/taxidetails"; //json node names private static final string tag_user = "taxi"; private static final string tag_name = "taxi_name"; private static final string tag_email = "taxi_plate_no"; jsonarray user = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btngetdata = (button)findviewbyid(r.id.getdata); btngetdata.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { new jsonparse().execute(); } }); } private class jsonparse extends asynctask<string, string, jsonobject> { @override protected void onpreexecute() { super.onpreexecute(); name1 = (textview)findviewbyid(r.id.name); plate1 = (textview)findviewbyid(r.id.plate); } @override protected jsonobject doinbackground(string... args) { httpconnection jparser = new httpconnection(); // getting json url jsonobject json = jparser.getjsonfromurl(url); return json; } @override protected void onpostexecute(jsonobject json) { try { // getting json array user = json.getjsonarray(tag_user); jsonobject c = user.getjsonobject(0); // storing json item in variable string name = c.getstring(tag_name); string email = c.getstring(tag_email); //set json data in textview name1.settext(name); plate1.settext(email); } catch (jsonexception e) { e.printstacktrace(); } } } }
httpconnection.java
public class httpconnection { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public httpconnection() { } 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; } }
i suggest adding volley project
https://developer.android.com/training/volley/index.html
and following example here https://developer.android.com/training/volley/request.html#request-json
you not need create own http request. let volley handle network request using jsonobjectrequest
Comments
Post a Comment