Patching with Retrofit - ID property only - android

I am making use of Retrofit in my app, it manages API calls well and supports GET and PATCH as I require.
Given the following JSON:
"fruit_set": [
{
"id": 2,
"fruit": {
"id": 1,
"name": "Apple",
"desc": ""
},
"source": 2,
"inventory": 8.0
},
{
"id": 6,
"fruit": {
"id": 2,
"name": "Mango",
"desc": "delicious"
},
"source": 2,
"inventory": 15.0
}]
Ignoring names for a minute.
I have setup a PoJo for both FruitSet and Fruit with the required getters/setters. FruitSet.Fruit is defined as List and is populated nicely with a GET call. My problem is with PATCH.
Fruit in this instance is reference data therefore I only need to insert the Fruit.id when assigning it to my FruitSet PoJo.
Can I instruct Retrofit that it doesn't need to write Fruit but only the Fruit.id to the database table. I was hoping Retrofit was smart enough to do that?
Basically I want this JSON structure when I PATCH:
"fruit_set": [
{
"id": 2,
"fruit": 1,
"source": 2,
"inventory": 8.0
},
{
"id": 6,
"fruit": 2,
"source": 2,
"inventory": 15.0
}]
Any ideas?

For those looking I implemented a custom Gson serializer that outputs only the ID instead of the entire Fruit object. This article really helped, especially towards the bottom:
http://www.javacreed.com/gson-serialiser-example/

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,...)

How to implement seat booking in Android?

I have this json response from the server:
[
{
"id": 12767236,
"rowNumber": "01",
"seatNumber": 1,
"status": "taken"
},
{
"id": 12767237,
"rowNumber": "01",
"seatNumber": 2,
"status": "available"
}
...
...
...
]
Basically it's a list of seats with a few properties, that's all the data I have. So, question is how do I show the seats and let user pick one like below? What component should I use?
You can use GridLayoutManager but you have to have a dummy/empty item to represent those rows, but you will need logic to assign those specific items to the list

http post request change the order of json object in ios (ionic 3)

I am working on an ionic app which will be used on android and iOS platforms.and I need to post an object to the server which contain some attribute that reference others for example:
{
"room": [
{
"#id": 2,
"date": "2019-10-10",
"number": "750"
}
],
"bed": [
[
{
"class": "A",
"room": 2
}
]
]
}
but when posting it the order of the JSON object changes to :
{
"bed": [
[
{
"class": "A",
"room": 2
}
]
],
"room": [
{
"#id": 2,
"date": "2019-10-10",
"number": "750"
}
]
}
what should I do to keep the order of the object, considering that it works fine in android?
Referring to JSON's standards, an object is simply unordered which means there is no problem with your case. However, if in your case the order is important, you may use arrays. Arrays make sure the order is preserved.

How to get git commit notes between two Bamboo build

I'm trying to set up CI for Gradle based Android project with Bamboo. Tutorial here work just work great for successful build.
For release note following I figure out to get Git log between two revision number.
git log ${bamboo.repository.previous.revision.number}..${bamboo.repository.revision.number}
But how to get last successful build git_revision number & current one.
Any suggestion ?
This involves scripting and utilizing REST API of Bamboo https://docs.atlassian.com/bamboo/REST [Choose the version you are working with]
To get all the build results, you need to make a call to:
[GET] <basepath>/rest/api/latest/result/{projectKey}-{buildKey}
Where, basepath is http://myhost.com:8085 OR http://myhost.com:8085/bamboo
Resulting in below:
{
"results": {
"size": 8,
"expand": "result",
"start-index": 0,
"max-result": 25,
"result": [
{
"link": {
"href": "<basepath>/rest/api/latest/result/{projectKey}-{buildKey}-{buildNumber}",
"rel": "self"
},
"plan": {
"shortName": "xyz",
"shortKey": "{buildKey}",
"type": "chain",
"enabled": true,
"link": {
"href": "<basepath>/rest/api/latest/plan/DS-ASVCCRED",
"rel": "self"
},
"key": "{projectKey}-{buildKey}",
"name": "ABCD",
"planKey": {
"key": "{projectKey}-{buildKey}"
}
},
"buildResultKey": "{projectKey}-{buildKey}-{buildNumber}",
"lifeCycleState": "Finished",
"id": 198039818,
"key": "{projectKey}-{buildKey}-{buildNumber}",
"planResultKey": {
"key": "{projectKey}-{buildKey}-{buildNumber}",
"entityKey": {
"key": "{projectKey}-{buildKey}"
},
"resultNumber": 45
},
"state": "Failed",
"buildState": "Failed",
"number": 45,
"buildNumber": 45
},
If JSON output is desired, just add Accept=application/json header while making a call.
This will return latest 25 build results in a sequence with the latest result being the first. You may go through this results and decide which two build results you are interested in.
Once you decide, you make additional calls to get change set (commit details) captured by bamboo for that particular build.
[GET] <basepath>/rest/api/latest/result/{projectKey}-{buildKey}/{buildNumber : ([0-9]+)|(latest)}?expand=changes
This will give you detailed commit description as below:
"changes": {
"size": 3,
"expand": "change",
"change": [
{
"author": "1234",
"changesetId": "7f76c41a7ff48f679a91d0fa2810ef3398121dc6"
},
{
"author": "abcd",
"changesetId": "104d8b7af9538599a02006005314033c8017e804"
},
{
"author": "cdef",
"changesetId": "d21aef9f3745257aa501425fc31ebd0c6b33f608"
}
],
"start-index": 0,
"max-result": 3
},
And then you can perform
git log <changesetId>...<changesetId>

Store Json from httpResponse into Android SQLite Database

How can I easily store this large amount of data into A SQLite database?
I read that I could use Gson to parse this data, but I'm not exactly sure how to do that.
I already have created a SQLiteHelper and the necessary classes, I am just stuck on how to parse this data.
I am getting a httpResponse that returns a large amount of Json that looks like this:
"objs": {
"ptoStatus": [
{
"id": 1,
"modifieddate": "2006-07-06 05:35:38",
"description": "Submitted",
"name": "Submitted",
"createddate": "2007-07-06 09:43:38"
},
{
"id": 2,
"modifieddate": "2006-07-06 09:35:38",
"description": "Approved",
"name": "Approved",
"createddate": "2009-07-06 09:35:38"
},
{
"id": 3,
"modifieddate": "2009-07-06 09:50:38",
"description": "Denied",
"name": "Denied",
"createddate": "2009-07-06 09:35:38"
}
],
"alertStates": [
{
"id": 1,
"createddate": "2008-02-11 09:11:57",
"modifieddate": "2008-02-11 09:11:57",
"description": "Sending"
},
{
"id": 2,
"createddate": "2008-02-11 09:11:57",
"modifieddate": "2008-02-11 09:11:57",
"description": "Response"
},
{
"id": 3,
"createddate": "2008-02-11 09:11:57",
"modifieddate": "2008-02-11 09:11:57",
"description": "Acknowledge"
},
//etc....
Retrofit is a great REST client for android applications. Here you can find the documentation:
http://square.github.io/retrofit/
You can use it for getting the http responses, but with it you can just turn all your response data into Java objects (by default it uses Googles Gson parser for that)
For that particular json you need to create something like this:
public class ObjsResponse {
public MyObjects data;
public class MyObjects {
List<States> ptoStatus;
List<AlertStates> alertStates;
}
}
And the models you store in the database just need to have the same names (or you can use annotations for different keys in the json, but that is all documented at Gson), like this:
public class States {
int id;
String modifieddate; //here you can use some sort of DateTime too (for example jodatime)
String description;
String name;
String createddate;
}
And here you can use the database annotations or anything you like.
After this you just have to set this ObjsResponse class as the return param to the retrofit interface (described at the upper link), have it initialized, then you can call this method. I don't write down the hole thing, in the documentation's Introduction section is basically all you need ;)

Categories

Resources