c# - Retrieve data from remote sqlserver to android app -
i want display data remote sql-server in android app. using web-service. able connect , insert not display json.
this code web-service
public datatable requestdetails(string request_name) { datatable requestdetails = new datatable(); requestdetails.columns.add(new datacolumn("request id", typeof(string))); requestdetails.columns.add(new datacolumn("date", typeof(string))); if(dbconnection.state.tostring() == "closed") { dbconnection.open(); } string query = "select id_requests,request_date requests request_by='" + request_name + "'"; sqlcommand command = new sqlcommand(query, dbconnection); sqldatareader reader = command.executereader(); if (reader.hasrows) { while (reader.read()) { requestdetails.rows.add(reader["request id"], reader["date"]); } } reader.close(); dbconnection.close(); return requestdetails; }
this android code:
protected class asyncloaddeptdetails extends asynctask<depttable, jsonobject, arraylist<depttable>> { arraylist<depttable> depttable = null; @override protected arraylist<depttable> doinbackground(depttable... params) { // todo auto-generated method stub restapi api = new restapi(); try { jsonobject jsonobj = api.requestdetails(params[1].getname()); jsonparser parser = new jsonparser(); depttable = parser.parsedepartment(jsonobj); } catch (exception e) { // todo auto-generated catch block log.d("asyncloaddeptdetails", e.getmessage()); } return depttable; } @override protected void onpostexecute(arraylist<depttable> result) { // todo auto-generated method stub (int = 0; < result.size(); i++) { data.add(result.get(i).getno() + " " + result.get(i).getname()); } adapter.notifydatasetchanged(); toast.maketext(context, "loading completed", toast.length_short).show(); } }
and jsonparser code:
public arraylist<depttable> parsedepartment(jsonobject object) { arraylist<depttable> arraylist=new arraylist<depttable>(); try { jsonarray jsonarray=object.getjsonarray("value"); jsonobject jsonobj=null; for(int i=0;i<jsonarray.length();i++) { jsonobj=jsonarray.getjsonobject(i); arraylist.add(new depttable(jsonobj.getint("request id"), jsonobj.getstring("date"))); } } catch (jsonexception e) { // todo auto-generated catch block log.d("jsonparser => parsedepartment", e.getmessage()); } return arraylist; }
i use fiddler or similar , have @ data being returned. i've had trouble returning .net datatable service, if me convert datatable json before sending. has been discussed on several posts before, here's great answer: https://stackoverflow.com/a/17398078/3299157
Comments
Post a Comment