Save objects into Android Realm DB on api call - android

I want to store objects received from API call into realm db.
Sample Obj:
[
{
"obj_type": "ClassSession",
"_id": "0341bd22-1182-4db3-a667-c8647012741e",
"version": "b2",
"createdOn": "2017-04-01 12:57:39 +0530",
"stage": "SCHEDULED",
"type": "Class Session",
"scheduled_at": "2017-04-01 12:58:39 +0530"
},
{
"obj_type": "Assignment",
"_id": "CmsRsrc2",
"version": "b3",
"name": "Homework",
"type": "HOMEWORK",
"parent": {
"type": "topic",
"id": "parent_id1"
},
"resource_id": "CmsRsrc2",
"assignmentProperty": {}
},
{
"obj_type": "Task",
"_id": "21b72bc0eb3e6d4a1c2bb332593ee19bbc527c92c9249585bed785fc6339e155",
"version": "b0",
"source": {
"type": "T_PROBLEM",
"id": "problem_id1_1"
},
"correctAnswer": 1
},
{
"obj_type": "TaskRecord",
"_id": "ff0748aea74431786a20629ad57676fc84eaa7396b153206540ee9eac1d9bfc8",
"assignmentId": "CmsRsrc2",
"version": "b0/",
"status": "ACTIVE",
"marking_scheme": {},
"sequence": 1
}
]
I have realm class for each obj_type but I don't know how to receive the obj from api as the response obj received from api does not belong to any realm class.
So I am looking for a way to receive the obj sent by api and save it to corresponding realm class.

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 API relationships with moshi

I'm having this response JSON that theorically follows the JSON API specs. I'm trying to parse with the moshi-jsonapi library but I don't know how to parse the some_objects relationship. In the SomeType class I have a member HasMany<SomeObject> someObjects and the class SomeObject is annotated in a proper way:
#JsonApi(type = "some_objects")
public class SomeObject extends Resource {
//....
}
But, after doing the parsing, I'm getting the someObjects member as null. Anyone knows why?
The JSON is that one:
"links": {
"self": "someurl/params"
},
"data": [
{
"type": "some_type",
"id": "12345",
"attributes": {
"attr1": 1,
"attr2": 2,
"attr3": 3
},
"relationships": {
"some_objects": {
"data": [
{
"type": "some_objects",
"id": "1"
},
{
"type": "some_objects",
"id": "2"
}
]
}
}
}
],
"included": [
{
"type": "some_objects",
"id": "1",
"attributes": {
"id": "1",
"parentId": "1"
},
"relationships": {
"subobjects": {
"data": [
{
"type": "subobjects",
"id": "2"
}
]
}
}
{
"type": "subobjects",
"id": "2",
"attributes": {
"metadata": {
"code": "AA"
},
"id": "2",
"parentId": "1"
}
}
],
"meta": {
"total": 1,
"totalCount": 1,
"correction": []
}
}
The only problem was the name of the member, after changing someObjects by 'some_objects' it worked like a charm.

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.

skipping null values when parsing json in android?

I am parsing json object from facebook.
In my facebook json object, there are string keys inside key array "data".
My Code as follows,
"data": [
{
"id": "100001211447563_300696056647440",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee is now using Facebook in English (US).",
"story_tags": {
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"type": "status",
"created_time": "2012-01-19T09:13:04+0000",
"updated_time": "2012-01-19T09:13:04+0000",
"comments": {
"count": 0
}
},
{
"id": "100001211447563_298802933503419",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee started using Graph API Explorer.",
"story_tags": {
"23": [
{
"id": 145634995501895,
"name": "Graph API Explorer",
"offset": 23,
"length": 18
}
],
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"picture": "http://photos-a.ak.fbcdn.net/photos-ak-snc1/v43/11/145634995501895/app_1_145634995501895_4870.gif",
"link": "http://developers.facebook.com/tools/explorer/",
"caption": "A tool to help you browse objects within the Facebook Graph API, manage permissions, obtain access tokens and generally learn how it all works.",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/100001211447563/posts/298802933503419"
},
{
"name": "Like",
"link": "http://www.facebook.com/100001211447563/posts/298802933503419"
}
],
"type": "link",
"created_time": "2012-01-16T09:47:57+0000",
"updated_time": "2012-01-16T09:47:57+0000",
"comments": {
"count": 0
}
},
{
"id": "100001211447563_298789650171414",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee likes myFBCovers.com.",
"story_tags": {
"15": [
{
"id": 160901873998019,
"name": "myFBCovers.com",
"offset": 15,
"length": 14
}
],
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"type": "status",
"created_time": "2012-01-16T08:57:33+0000",
"updated_time": "2012-01-16T08:57:33+0000",
"comments": {
"count": 0
}
}
when I parsing this json object, String key "likes" is sometimes exist and sometimes do not exist in "data" jsonArray. So when I parsing JsonString "likes", sometimes I gets JSONException since I am parsing null value. Is there any way to skipping "null" json value?
Please help me regarding this.
if try catch it, next processes will be skipped.
you can check by isNull("name") method.
Edit: this is to check key. since you mentioned key exists sometimes. did you mean the value?
use JSONObjects method has(key)
data = (JSONObject) new JSONTokener(jsonObjRecv.toString()).nextValue();
if data.has("like"){
...
}
to check whether the value is null check with isNull("name")

api - get feed "with_tags"

I have a problem getting my feeds.
1- I (emiliano nunez) post a photo with my cellphone (android) and add (With) "hall 9000".
I (hall 9000) get feeds by graph api:
https://graph.facebook.com/me/feed?access_token=..........
{
"from": {
"name": "Emiliano Gustavo Nu\u00f1ez",
"id": "100001535156886"
},
"type": "photo",
"picture": "http://photos-a.ak.fbcdn.net/hphotos-ak-snc7/386161_265139050213931_100001535156886_723669_443588989_s.jpg",
"message": "Ibrick test",
"with_tags": {
"data": [
{
"name": "Hal Nuevemil",
"id": "100003200111123"
}
]
},
"to": {
"data": [
{
"name": "Hal Nuevemil",
"id": "100003200111123"
}
]
},
"id": "100001535156886_265139076880595",
"created_time": "2011-12-29T18:43:49+0000"
}
I can see "with_tags" section with hall 9000 :)
Until now perfect..
2 - then i repeat the procedure with other photo ... and get:
{
"from": {
"name": "Emiliano Gustavo Nu\u00f1ez",
"id": "100001535156886"
},
"type": "photo",
"picture": "http://photos-e.ak.fbcdn.net/hphotos-ak-snc7/396071_265171810210655_100001535156886_723868_1962904772_s.jpg",
"id": "100001535156886_265171840210652",
"created_time": "2011-12-29T19:44:56+0000"
},
I don't see "with_tags" section.
:(
For more details:
http://i.stack.imgur.com/N4knz.png
http://i.stack.imgur.com/uPqvh.png

Categories

Resources