android - how to filter data while retrieving from mongoDB hosted on mlab -
rest api codecomplete displayfitactivity code1. code retrieve data mlab , displaying in android app. 2. have used restful api connect mlab ref->youtube.com/watch?v=pi9fsrp2fyo&t=954s 3. have 4 columns in collection "bookname": "spcc", "author": "john", "price": 1234, "stock": 5, //ref mlab want filter data author name or price > 50 , price <200 etc. data(filtering condition) user in android app.so how write in method documents satisfies user filtering conditions ?//sorry if basic question
private string getdata(string urlpath) throws ioexception { // n.settext("get data"); stringbuilder result = new stringbuilder(); bufferedreader bufferedreader =null; try { //initialize , config request, connect server url url = new url(urlpath); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setreadtimeout(10000 /* milliseconds */); urlconnection.setconnecttimeout(10000 /* milliseconds */); urlconnection.setrequestmethod("get"); urlconnection.setrequestproperty("content-type", "application/json");// set header urlconnection.connect(); //read data response server inputstream inputstream = urlconnection.getinputstream(); bufferedreader = new bufferedreader(new inputstreamreader(inputstream)); string line; while ((line = bufferedreader.readline()) != null) { result.append(line).append("\n"); } } catch (jsonexception e) { e.printstacktrace(); } { if (bufferedreader != null) { bufferedreader.close(); } } return result.tostring(); } and string result passed postexecute displaying data
@override protected void onpostexecute(string result) { super.onpostexecute(result); list<books> data=new arraylist<>(); jsonarray obj ; try { obj = new jsonarray(result); //obj = new jsonobject(result); cn = new string[obj.length()]; books[] bookdata = new books[obj.length()]; (int = 0; < obj.length(); i++) { jsonobject jsonobj = obj.getjsonobject(i); // books bookdata = new books(); string n1; bookdata[i] = new books(); bookdata[i].id = jsonobj.getstring("_id"); bookdata[i].bookname = jsonobj.getstring("bookname"); bookdata[i].authorname = jsonobj.getstring("author"); bookdata[i].price = integer.parseint(jsonobj.getstring("price")); bookdata[i].stock = integer.parseint(jsonobj.getstring("stock")); data.add(bookdata[i]); if (i == obj.length() - 1) { n.settext("total row = " + obj.length()); }
Comments
Post a Comment