Dynamic fields data save to web services in android - android

I am getting multiple fields dynamically from JSON data. To save data of dynamic fields into web services i prefer to call by id. But i unable to get the id of that fields. So I need to know how to get ids of dynamic fields.
Here is my JSON response:
{
"sections": [
{
"head": "Section A",
"sort": 1,
"section_id": 3,
"cols": "2",
"fields": [
{
"id": 5,
"template_id": 2,
"section_id": 3,
"label": "A1",
"itype": "1",
"idefault": "",
"ioptions": "",
"required": "N",
"ivalids": "0",
"sort": 1,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "2018-09-23 05:32:59",
"deleted_at": null
},
{
"id": 6,
"template_id": 2,
"section_id": 3,
"label": "A2",
"itype": "1",
"idefault": "",
"ioptions": "",
"required": "N",
"ivalids": "0",
"sort": 1,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "2018-09-23 05:32:59",
"deleted_at": null
}
]
}
]
}
If "itype"=1 dynamic_field = edittext, "itype"=2 dynamic_field =
spinner and so on.
In my response, I am getting "itype"=1 so i am dynamically displaying two edit texts.
And "label" from JSON used as id for a dynamic field. If I integrate API, it's getting the error as "can't resolve the id".
I don't know how to resolve this error.
Screenshot

Related

Retrofit Android API Get without nested array

This is my first android project (I'm a php developer) but i'm struggling to see how to take this example which uses a nested structure under data for the json array whereas my data is not double nested like this example.
The excample code I am trying to modify is from here https://github.com/datanapps/RetrofitKotlin and the key differences in the json are todo with the nesting.
Original
{
"page": 1,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 1,
"email": "george.bluth#reqres.in",
"first_name": "George",
"last_name": "Bluth",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
},
{
"id": 2,
"email": "james.smith#reqres.in",
"first_name": "James",
"last_name": "Smith",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
}
]
}
whereas mine is simply
[
{
"_id": "62f4cb4e131d9d00046a1b9c",
"device": "AndroidAPS-DexcomG6",
"date": 1660209967000,
"dateString": "2022-08-11T09:26:07.000Z",
"isValid": true,
"sgv": 133,
"direction": "Flat",
"type": "sgv",
"created_at": "2022-08-11T09:26:38.326Z",
"mills": 1660209998326
},
{
"_id": "62f4cb4e131d9d00046a1b9c",
"device": "AndroidAPS-DexcomG6",
"date": 1660209967000,
"dateString": "2022-08-11T09:26:07.000Z",
"isValid": true,
"sgv": 133,
"direction": "Flat",
"type": "sgv",
"created_at": "2022-08-11T09:26:38.326Z",
"mills": 1660209998326
}
]
Specifically I want to query the SGV values, direction and date
Thanks
You "return type" of "method" in Retrofit "interface" will be:
List<MyModel>
And MyModel:
data class MyModel(val _id:String, val device:String,...)

Json Data Filteration according to category

I want to know how to filter data. I got data with category name but cannot filter category wise.Here is my data:
{
"TITLE": "tea",
"PRICE": "17",
"QTY": "1",
"CAT_ID": "33",
"CAT_NAME": "POPULAR PRODUCTS",
"TYPE": "veg"
},
{
"TITLE": "Coffee",
"PRICE": "102",
"QTY": "1 ",
"CAT_ID": "8",
"CAT_NAME": "MORNING SPECIAL",
"TYPE": "veg"
},
{
"TITLE": "Jeera chachh",
"PRICE": "42",
"QTY": "1",
"CAT_ID": "33",
"CAT_NAME": "POPULAR PRODUCTS",
"TYPE": "veg"
},
{
"TITLE": "Roti Ghee Wali",
"PRICE": "21",
"QTY": "1",
"CAT_ID": "33",
"CAT_NAME": "POPULAR PRODUCTS",
"TYPE": "veg"
},
{
"TITLE": "Paneer paratha",
"PRICE": "102",
"QTY": "1 ",
"CAT_ID": "8",
"CAT_NAME": "MORNING SPECIAL",
"TYPE": "veg"
},
{
"TITLE": "Mix paratha",
"PRICE": "102",
"QTY": "1",
"CAT_ID": "8",
"CAT_NAME": "MORNING SPECIAL",
"TYPE": "veg"
},
I already parse the data but not able to set in fragment according to category.Can anyone solve my issue.
Step 1: Store the data in database.
Step 2: Search
select * from table where CAT_NAME = 'your_filter_category'.
or better ....CAT_NAME like %your_filter_category%.
In Case of no database;
Store the Data in list.
HashMap<String, ArrayList<Item>> map = new HashMap<>();
ArrayList<Item> itemList = new ArrayList<>();
Loop on List{
Item item = fromLoop index;
if(map.contains(item.getCat())){
itemList = map.get(item.getCat());
itemList.put(item);
map.put(item.getCat(),itemList);
}else{
itemList.clear();
itemList.put(item);
map.put(item.getCat(),itemList);
}
}
//after loop you will get HashMap with keys as categories and Values as the list of selected key category.
Step 3: Publish the results.
Note: No one will do your homework here. the only thing we can do for you, we can assist you how to do.
enter your code to: JSON Formatter and you will see that something is invalid in your JSON, it has multiple root elements. Maybe you should store whole code in JSONArray like this: `
{
"features":
[{
"TITLE": "tea",
"PRICE": "17",
"QTY": "1",
"CAT_ID":"33",
"CAT_NAME": "POPULAR PRODUCTS",
"TYPE": "veg" },
{
"TITLE":"Coffee",
"PRICE": "102",
"QTY": "1 ",
"CAT_ID": "8",
"CAT_NAME":"MORNING SPECIAL",
"TYPE": "veg"
}]
}

How to pass object of json to other activity

I have this json
{
"results": [{
"gender": "male",
"name": {
"title": "mr",
"first": "isidro",
"last": "nogueira"
},
"location": {
"street": "3663 rua amazonas ",
"city": "mesquita",
"state": "rio de janeiro",
"postcode": 27744
},
"email": "isidro.nogueira#example.com",
"login": {
"username": "orangegoose315",
"password": "redalert",
"salt": "Dd3U7U03",
"md5": "325f1e8753222facba6ee63a5e2451de",
"sha1": "a63a0c544a956e235330b0ed76d11d3ad17c7979",
"sha256": "ddf18ffff89684617b2c7b8c5c175b9570cb6005916d9b4f3face802bd4f13a1"
},
"dob": "1967-04-23 04:58:32",
"registered": "2016-05-08 01:56:44",
"phone": "(11) 8368-8583",
"cell": "(17) 9743-3074",
"id": {
"name": "",
"value": null
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/57.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/57.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/57.jpg"
},
"nat": "BR"
}],
"info": {
"seed": "5b3dff5c25caaf7f",
"results": 1,
"page": 1,
"version": "1.1"
}
}
This data is of just one user. I have 50 of them.
I am still in learning stage and what I have done is that I have successfully listed all user in an activity. Now what I want to do is when I click on a user it should show it's related details in next activity...
List Users->Select Users->Show selected user details
I still don't know how to send data to other activity and display it.

RetroFit, Many to many relationships

I have a json response that looks somewhat like this.
{
"id": 1,
"username": "user",
"email": "generic user#gmail.com",
"name": "harshvardhan",
"phone_no": 2147483647,
"created_at": "2016-06-27 12:23:25",
"updated_at": "2016-06-27 12:23:25",
"hash": "",
"groups": [
{
"id": 37,
"title": "aspernatur-et-rem-quos-eius-voluptatem-eveniet-aut",
"descr": "33",
"identifier": "5771545383a2d",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:07",
"pivot": {
"user_id": 1,
"grp_id": 37,
"roles": "admin"
}
},
{
"id": 67,
"title": "quibusdam-voluptas-non-facere-nihil",
"descr": "194",
"identifier": "57715453b8d80",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:07",
"pivot": {
"user_id": 1,
"grp_id": 67,
"roles": "admin"
}
},
{
"id": 161,
"title": "libero-id-ipsa-beatae-aut",
"descr": "59",
"identifier": "5771545469db4",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:08",
"pivot": {
"user_id": 1,
"grp_id": 161,
"roles": "admin"
}
},
{
"id": 198,
"title": "iure-ad-sunt-id-delectus-laboriosam-quo",
"descr": "150",
"identifier": "57715454a9d15",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:08",
"pivot": {
"user_id": 1,
"grp_id": 198,
"roles": "admin"
}
}
]
}
There is a many to many relationship between users and groups.
I am using retrofit with the GsonConverterFactory. On response, how can I parse this information into objects?
I tried making a User class , and a group class, then I put an array of groups as a member of the user class.
But when I access User.getGroups().get(1).getID , it returns 0.
However, User.getGroups().size() does return 4 (the correct number in this case)
How can I achieve this in retrofit?
A practical tool to parse JSON correctly with retrofit can be to generate the classes with http://www.jsonschema2pojo.org
You just have to put the example JSON, the resource type in JSON and put the desired annotation style (GSON, Jackson, ..).
Finally click on the preview button and you can see the results

Parsing Boolean JSON value is always giving me false

I am getting the following JSON:
{
"ID": "479",
"id": "479",
"name": "Nishant Kango",
"last_logged": "1461225061",
"current_status": "icon-online prople-online",
"room_id": "d18f655c3fce66ca401d5f38b48c89af",
"current_user": false,
"lastmessage": [],
"userInfo": {
"id": "479",
"birthday": "1989-11-21",
"comment_privacy": "everyone",
"confirm_followers": "1",
"current_city": "Chandigarh, Chandigarh",
"follow_privacy": "everyone",
"gender": "male",
"hometown": null,
"message_privacy": "everyone",
"timeline_post_privacy": "a:1:{i:0;s:1:\"1\";}",
"feed_section": "1",
"post_privacy": "a:1:{i:0;s:1:\"5\";}",
"current_city_id": "1433",
"userSkills": ", Testing ",
"birth": {
"date": "21",
"month": "11",
"year": "1989"
},
"about": "asdasdasdas sad asd asd asd as",
"active": "1",
"avatar_id": "477",
"cover_id": "528",
"cover_position": "465",
"email": "nishantkango#yahoo.co.in",
"email_verification_key": null,
"email_verified": "0",
"language": null,
"last_logged": "1461225061",
"name": "Nishant Kango",
"time": "0",
"timestamp": "2016-04-21 13:19:40",
"timezone": null,
"type": "user",
"username": "nishant",
"verified": false,
"user_id": "479",
"first_name": "Nishant",
"middle_name": null,
"last_name": "Kango",
"dob": "1989-11-21",
"mobile": "8699022278",
"title": null,
"marital_status": "single",
"childrens": "0",
"hobbies": "Travelling",
"interests": "Technology",
"feed_back": null,
"status": "approved",
"profile_pic": null,
"date_created": "2014-10-29 00:00:00",
"date_modified": "2016-03-31 19:07:56",
"activation_code": null,
"social_login_type": "facebook",
"facebook_id": "10205077190793111",
"linked_in_id": null,
"google_id": null,
"employment_status": null,
"is_deleted": "0",
"website": "http://www.nishantkango.com",
"reference_institute": "",
"industry": "0",
"proffesion_type": "",
"url": "http://192.168.2.250:82/user/timeline/e6b34e9a0311a7f829e09d2d7c4b313e",
"cover": {
"id": "528",
"active": "1",
"album_id": "0",
"extension": "jpg",
"name": "mentordirectorybgimagesuggestions6.jpg",
"post_id": "0",
"temp": "0",
"timeline_id": "0",
"type": "photo",
"url": "common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c",
"complete_url": "http://192.168.2.250:82/common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c.jpg",
"post_url": "/index.php?tab1=story&id=0"
},
"actual_cover_url": "http://192.168.2.250:82/common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c.jpg",
"cover_url": "http://192.168.2.250:82/common/marvel/photos/2016/02/hqUDT_528_f4be00279ee2e0a53eafdaa94a151e2c_cover.jpg",
"avatar": {
"id": "477",
"active": "1",
"album_id": "0",
"extension": "jpg",
"name": "10390233_10205765787487598_5674312231016706405_n.jpg",
"post_id": "0",
"temp": "0",
"timeline_id": "0",
"type": "photo",
"url": "common/marvel/photos/2016/01/yS1xw_477_74071a673307ca7459bcf75fbd024e09",
"complete_url": "http://192.168.2.250:82/common/marvel/photos/2016/01/yS1xw_477_74071a673307ca7459bcf75fbd024e09.jpg",
"post_url": "/index.php?tab1=story&id=0"
},
"thumbnail_url": "http://192.168.2.250:82/common/marvel/images/default-male-avatar.png",
"avatar_url": "http://192.168.2.250:82/common/marvel/images/default-male-avatar.png",
"online": true
}
}
I have to check the online/offine status of a user using online key inside the userInfo object.I am using the following code :
JSONObject userInfo = object.getJSONObject("userInfo");
Boolean online = userInfo.getBoolean("online");
Log.e("Online",online);
When i tried to use this statement :
Log.e("online1", (String) userInfo.get("online"));
It gives me the error that java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String .
So it is sure that userInfo.get("online") is returning me some boolean value.So when i use this :
Log.e("online1", String.valueOf(userInfo.getBoolean("online")));
it gives me always false but i know i am getting true value from server in some cases.Please help me to fix the issue
Are you certain the get() function returns a boolean. I am not certain about the JSON library with Android but ones I have used will return the value as a string for which you then need to parse using Boolean.parseBoolean(value).
Hope that helps.
You can try
Boolean online = Boolean.parseBoolean(userInfo.getString("online"))
This is worked for me.
you try boolean online = userInfo.getBoolean("online"); instead of Boolean online = userInfo.getBoolean("online");
I'd recommend using the opt version instead of the get version.
The main difference is that opt will not throw a JSONException and stop parsing.
Try therefore to replace all of your getXXX() by optXXX().
Another upside is that with opt, you can specify a fallback value.
Log.e("Online", online.toString());
It takes only String.
The method e(String, String) in the type Log is not applicable for the arguments (String, boolean).

Categories

Resources