I successfully got my Android app to sync with my iriscouch database. In this database I have about 100 documents, each representing one line of public transport with its corresponding stations, like:
{
"_id": "1",
"_rev": "1-9658663cc48789829fb505ce2e0fd4ce",
"name": "1",
"stations": [
{
"_id": "Stefan-Fadinger-Platz",
"name": "Stefan-Fadinger-Platz",
"long": "16.35464100",
"lat": "48.16771000"
},
{
"_id": "Julius-Raab-Platz",
"name": "Julius-Raab-Platz",
"long": "16.38323500",
"lat": "48.21135300"
},
{
"_id": "Schwedenplatz U",
"name": "Schwedenplatz U",
"long": "16.37765200",
"lat": "48.21178500"
},
....
}
How can I access this data from Android based on user selection, for example getting all stations for line 1?
As far as I've read, I need to create a view for each query, but I don't think manually creating 100 views is performant. Is there a dynamic way?
You can create views locally in TouchDB.
http://wiki.apache.org/couchdb/HTTP_view_API
They work the same as regular CouchDB views. You can provide parameters to the views that help you filter based on a user selection.
Related
I want to make a categories screen, the backend returns a nested json with 4 trees in total if I'm not mistaken,
example:
when I press the most general tree of the categories, I would like it to be displayed as a dropdown and there, put the children of this category, the ones that come in the json clearly, hope to explain myself well.
I have to fill these widgets, their text or titles with the text that comes clearly in the json, but I'm not going to be calling the endpoint every time I want to paint the titles, i have to save what comes in the json in the sqlite, and then extract from there the texts to put on categories screen ... how can I do this? I'm new, never build a structure like that, I have to do it with sqlite, but I wouldn't know how to start or how to build the tables, if I make a table, only the first tree of the categories will go, and the rest? the children of the nested json? Could someone guide me?
I leave you a piece of the json (for other reasons I can not put it complete) to put them a little more in context
{
"res": 0,
"categorias": [
{
"id": 1,
"name": "Bolsas",
"img": "hogar.png",
"titulo": 0,
"children": [
{
"id": 11,
"name": "Para llevar",
"img": "llevar.png",
"titulo": 0
},
{
"id": 12,
"name": "Para Conservar",
"img": "consrvar.png",
"titulo": 0
},
{
"id": 13,
"name": "Para Organizar",
"img": "organizar.png",
"titulo": 0
},
{
"id": 14,
"name": "Multipack",
"img": "multipack.png",
"titulo": 0
}
]
},
{
"id": 2,
"name": "Cuidado del aire",
"img": "aire.png",
"titulo": 0,
"children": [
{
"id": 21,
"name": "Instantaneo",
"img": "instantaneo.png",
"titulo": 0,
"children": [
{
"id": 211,
"name": "Aerosol",
"img": "llevar.png",
"titulo": 0
},
{
"id": 212,
"name": "Toque",
"img": "consrvar.png",
"titulo": 0
}
]
},
You have several option to work with json and sqlite:
Just put your json as string in row on table and then select as string and convert to json.
You can use similar approach from paragraph 1 bud with using sqlite's json extension. It adds possibility to select of update parts of json by special query syntax. To use sqlite with this extension u will need custom build of sqlite like sqlite-android (with have many other benefits).
You can crate separate sqlite table from each level fo json nesting (depends on your need of granularity). For example you can create table categorias with columsn(categorias_id, categorias_name, categorias_img, categorias_titulo), table categorias_childrens with columns(categorias_id, id, name, img, titulo, categorias_childrens_id).
In this case all rows of categorias_childrens table related with categorias.categorias_id contains categorias.categorias_id in categorias_childrens.categorias_id column. And all rows related with some of categorias_childrens row contains categorias_childrens.categorias_id in categorias_childrens.categorias_childrens_id column (situation when children.id.21 has childrens ).
Or u can combine approaches fro example: create table categorias with columsn(categorias_id, categorias_name, categorias_img, categorias_titulo, children) where column children will be contains string json value.
I have established a connection but unable to access data.Help needed
JSON data
[
{
"name": "Margherita",
"price": 170
},
{
"name": "Corn MAnia",
"price": 170
},
{
"name": "Triple Tango",
"price": 210
}
]
In MarkLogic Server, you can use the XPath language to traverse JSON and retrieve data. Code can be written in the Xquery language or Server Side JavaScript:
xquery version "1.0-ml";
let $doc:= xdmp:unquote('{"root":[{"name": "Margherita","price": 170}, {"name": "Corn MAnia","price": 170}, {"name": "Triple Tango","price": 210}]}')
return xdmp:document-insert('/doc/names.json', $doc)
To get data, use:
doc('/doc/names.json')/root[1]/name
or
doc('/doc/names.json')/root[2]/price/data()
friends:
I get a response from remote server as follows on Android:
{
"items": {
"persons": [
{
"id": "200120",
"name": "Bill"
},
{
"id": "200121",
"name": "Jim"
}
],
"tasks": [
{
"id": "001",
"name": "Fetch ten books",
"deadline": "5:30 p.m.",
"scores": "10"
},
{
"id": "002",
"name": "Fetch thirty books",
"deadline": "5:30 p.m.",
"scores": "30"
}
],
"intro": "This is a funny game."
},
"otherObj": []
}
And I want to save it to phones. I do not think it a good choice to save it into databases. However it read not fast if put the response to SharedPreferences file. Is there any other way?
No need to save this data into database as these value are dynamic and will change time to time.
In your Activity, call the API and get JSON as response and then parse this JSON and show it on device.
To store it on device u can use Singleton class also.
For JSON parser refer to http://www.tutorialspoint.com/android/android_json_parser.htm
You can first save your data into objects and at last into Object Array and then Save to database.(If your data is dynamic it is better you only deal with Object not database)
For saving to database you can use
db.beginTransaction();
for(object : ObjectArray){
db.dboperation();
}
db.setTransactionSuccessful();
db.endTransaction();
I have a ListView that is using a custom adapter to populate its rows.
I want to get two String values from the adapter that I have clicked, so I used getItem method:
#Override
public UserItemsModel getItem(int i) {
return arrUserItems.get(i);
}
As you can see above, I'm suing UserItemsModel as a reference to get the item id.
So, in the list fragment, I retrieve the values I desired by using onListItemClicked method:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
UserItemsModel userItemsModel = (UserItemsModel) l.getAdapter().getItem(position);
String item_id = userItemsModel.getItem_id();
//String user_id = userItemsModel.getUser_id(); This is not in the UserItemsModel
mListener.onUserItemSelected(user_id, item_id);
}
However, the user_id is not in the UserItemsModel (as I have modeled from a Json format), instead user_id is in the other model that the adapter doesn't use.
UserItemsModel look like this:
public class UserItemsModel {
private String item_id; // i can get this
private String item_name;
No user_id that I wanted. But, it is in UserShopModel which is a parent of UserItemsModel
public class UserShopModel {
private String user_id; // i can't get this
private String username;
private String name;
When I run the app, it throws an error that says something like Cannot Cast UserShopModel into UserItemsModel
Anyone know how to get around this? If anything unclear please let me know.
UDPATE:
Here is how the JSON look like:
{
"results": {
"user_id": "2947",
"username": "100001235797881",
"name": "Fast Reload",
"email": "atiqahfatin76#yahoo.com",
"phone": "6Lparw1NrMmpu7gd2U18mg==",
"avatar": "http:\/\/ked.ai\/uploads\/avatar\/2947_avatar.png",
"shop_name": "FastReload",
"shop_desc": "our vision is to provide Fast And Easy reload service in malaysia. After One time registeration You can top up anytime anywhere at any number with lower price using sms system.24\/7, no internet connection required. Click here http:\/\/www.fastreload.tk\/ For more info and registeration .Thank You =)",
"shop_namespace": "FastReload",
"shop_canvas": "http:\/\/ked.ai\/uploads\/canvas\/2947_canvas.png",
"profile": [
{
"id": "1",
"title": "Shop Name",
"content": "FastReload"
},
{
"id": "2",
"title": "Shop Namespace",
"content": "FastReload"
},
{
"id": "3",
"title": "Shop Description",
"content": "our vision is to provide Fast And Easy reload service in malaysia. After One time registeration You can top up anytime anywhere at any number with lower price using sms system.24\/7, no internet connection required. Click here http:\/\/www.fastreload.tk\/ For more info and registeration .Thank You =)"
},
{
"id": "4",
"title": "Customer Service",
"content": "operation hours 9 am to 9 pm"
},
{
"id": "5",
"title": "Trust Level",
"content": "0%"
},
{
"id": "6",
"title": "Bank Details",
"content": null
}
],
"items": [
{
"item_id": "20321",
"name": "Fast And Easy Reload Service",
"price": "20",
"description": " our vision is to provide Fast And Easy reload service in malaysia. After One time registeration You can top up anytime anywhere at any number with lower price using sms system.24\/7, no internet connection required.Click here http:\/\/www.fastreload.tk\/ For more info and registeration .Thank You =)",
"category_id": "127",
"category": "Services",
"vanity": [
],
"thumbnail": {
"image50": "http:\/\/ked.ai\/uploads\/item\/100001235797881\/2947_1391259043.71a8deec82a92fc51acfa63c7a87c263_50.jpg",
"image100": "http:\/\/ked.ai\/uploads\/item\/100001235797881\/2947_1391259043.71a8deec82a92fc51acfa63c7a87c263_100.jpg"
}
}
],
"vanities": [
],
"categories": [
{
"category_id": "127",
"category_name": "Services",
"category_url": "http:\/\/ked.ai\/FastReload\/category\/services",
"count": "1"
}
]
},
"errors": ""
}
If you wish to obtain the user id from the ShopModel, then the only way you might want to do this, is:
1. Either pass the userShopModel as an argument to the constructor of the adapter.
The assumption you might want to make is, UserShopModel and UserItemModel
have the same number of items(that is the size is the same)
2. The other thing you could do is, inside every userItemModel create a property
of the type UserShopModel such that for every item that you click,
you not only get the item_id but also the user_id
Hope this helps.
Hi I have following JSON
{
"code": 0,
"response": {
"userObject": {
"User": {
"id": "355660",
"first_name": "Dummy",
"last_name": "dummy",
"email": "dumb#email.com",
"birthday": "2012-05-07",
"created": "2012-08-21 06:41:05",
"modified": "2012-08-21 06:41:05",
"image_url": null,
},
"Location": {
"id": "273550",
"name": "New York City",
"asciiName": "New York City",
"lat": "40.714272",
"lon": "-74.005966",
"geoname_modified": "2011-11-08 00:00:00",
"timeZone": "America/New_York",
"countryName": "United States",
"state": "New York",
"created": "2012-07-12 12:11:01",
"modified": "2012-08-20 14:27:24"
}
}
}
}
I have two classes, one each for Location and User
I know that I can get the objects if I create nested class like
response
->UserObject
*User
*Location
But i don't want to create two extra classes for UserObject and response just for wrapping the two POJO's .
Is there any simpler way to do it??
I am using Jackson Parser with Spring for android
You can also do it in two steps, if you really want to avoid throw-away classes, like:
JsonNode tree = mapper.readTree(...);
User user = mapper.treeToValue(tree.path("response").path("userObject").get("User"), User.class);
Location loc = mapper.convertValue(tree.path("response").path("userObject").get("Location"), Location.class);
but yeah I might go with silly struct-classes instead:
static class Response {
public UserObject userObject;
}
static class UserObject {
public Location Location;
public User User;
}
since it really isn't much more code.
Rather than creating classes you could create arrays or use hashmap. Personally, I would just create the classes. I think that this give you more flexibility in your app, and will allow you to work with the objects with less hassle. I know it takes time to set them up, but once you do that, you can use ArrayList and you can parse the JSON quite a bit easier.