How to write json Objects in order wise?Android - android

I am writing a json file to my external storage. Below code is used to create json objects and then adding them to json array
JSONObject jsonObj = new JSONObject();
jsonObj.put("slotid", "1a");
jsonObj.put("updatetime", 1477650798);
jsonObj.put("active", true);
jsonObj.put("adurl", "httpAd");
jsonObj.put("imgurl", "httpImg");
jsonObj.put("hd", false);
jsonObj.put("x", 0);
jsonObj.put("y", 0);
phoneArray.put(jsonObj);
jsobObj.put("slots", phoneArray);
Above code works fine for API24 but when i try to run same code for API 19 then following output occurs
{
"containers": [ ],
"slots": [
{
"slotid": "1a",
"updatetime": 1477650798,
"adurl": "httpAd",
"active": true,
"hd": false,
"y": 0,
"x": 0,
"imgurl": "httpImg"
},
{
"slotid": "1a",
"updatetime": 1477650798,
"adurl": "httpAd",
"active": true,
"hd": false,
"y": 0,
"x": 0,
"imgurl": "httpImg"
}
]
}
My question is why my jsonObjects are not writing in order? I am successfully getting correct output on API 24 i.e
{
"slots": [
{
"slotid": "1a",
"updatetime": 1477650798,
"active": true,
"adurl": "httpAd",
"imgurl": "httpImg",
"hd": "false",
"x": 0,
"y": 0
},
{
"slotid": "1a",
"updatetime": 1477650798,
"active": true,
"adurl": "httpAd",
"imgurl": "httpImg",
"hd": "false",
"x": 0,
"y": 0
}
],
"containers": []
}
Where should be the problem?

Related

Appium Android PointerInput LongPress code is needed

Faced error: Encountered internal error running command: io.appium.uiautomator2.common.exceptions.InvalidArgumentException: Exception while reading JSON
Have coded in java and showed the appium logs here.
My code:
{
"id": "finger",
"type": "pointer",
"parameters": {
"pointerType": "touch"
},
"actions": [
{
"duration": 0,
"type": "pause"
},
{
"duration": 0,
"x": 862,
"y": 747,
"type": "pointerMove",
"origin": "viewport"
},
{
"button": 0,
"type": "pointerUp"
}
]
}
]
[
{
"id": "finger",
"type": "pointer",
"parameters": {
"pointerType": "touch"
},
"actions": [
{
"duration": 0,
"type": "pause"
},
{
"duration": 0,
"x": 862,
"y": 747,
"type": "pointerMove",
"origin": "viewport"
},
{
"button": 0,
"type": "pointerUp"
}
]
}
]
You can use this function for long-press operation for the recent appium version without touch action and javascript.
Point element_location = WebElement.getLocation(); //Getting element's location
long_press(element_location);// passing the location coordinates to the long_press funcation
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence longpress = new Sequence(finger, 1);
longpress.addAction(finger.createPointerMove(Duration.ofMillis(0),PointerInput.Origin.viewport(), location.x, location.y));
longpress.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
longpress.addAction(finger.createPointerMove(Duration.ofMillis(2000),PointerInput.Origin.viewport(), location.x, location.y));
longpress.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(Collections.singletonList(longpress));

How to handle retrofit call that returns multiple models

I am making an API call that returns an array of distinct object that have different attributes:
[
{
"likesCount": 17771,
"sharesCount": 8823,
"liked": false,
"shared": false,
"mediaType": "video",
"mediaWidth": 1920,
"mediaHeight": 1080,
"mediaAspectRatio": "16:9",
"mediaVideoUriDash": "https://d339b3x89fwe5u.cloudfront.net/2685322c-52cb-4c45-b671-63e607f17c97/dash/community-tile-video-1600081282.mpd",
"mediaVideoUriHls": "https://d339b3x89fwe5u.cloudfront.net/2685322c-52cb-4c45-b671-63e607f17c97.m3u8",
"mediaVideoCoverImageUri": "https://d339b3x89fwe5u.cloudfront.net/2685322c-52cb-4c45-b671-63e607f17c97/thumbnails.jpg"
},{
"createdDate": "2020-09-14T11:01:20.025583Z",
"createdTimestampEpochInMilliseconds": 1600081280026,
"updatedDate": "2020-09-14T11:10:41.9218332Z",
"updatedTimestampEpochInMilliseconds": 1600081841922,
"id": "2490fc73-e17a-4a77-8905-b3e945c89fbf",
"name": "Test",
"url": "https://google.com/",
"categoryType": "community",
"weight": 5400,
"isLikable": true,
"isSharable": true,
"isFullWidth": true
}
]
How to I parse them into different local objects?

Suggest a Future builder method for the following json

{
"totalHits": 500,
"hits": [
{
"largeImageURL": "https://pixabay.com/get/52e3d6414352af14f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_1280.jpg",
"webformatHeight": 424,
"webformatWidth": 640,
"likes": 11,
"imageWidth": 4456,
"id": 4332903,
"user_id": 1151065,
"views": 697,
"comments": 14,
"pageURL": "https://pixabay.com/photos/evangelion-mech-unit-02-robot-4332903/",
"imageHeight": 2958,
"webformatURL": "https://pixabay.com/get/52e3d6414352af14f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_640.jpg",
"type": "photo",
"previewHeight": 99,
"tags": "evangelion mech unit 02, robot, fighter",
"downloads": 311,
"user": "vinsky2002",
"favorites": 0,
"imageSize": 878944,
"previewWidth": 150,
"userImageURL": "https://cdn.pixabay.com/user/2019/05/15/02-40-06-911_250x250.png",
"previewURL": "https://cdn.pixabay.com/photo/2019/07/12/13/44/evangelion-mech-unit-02-4332903_150.jpg"
},
{
"largeImageURL": "https://pixabay.com/get/55e7d54b4d51a514f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_1280.jpg",
"webformatHeight": 359,
"webformatWidth": 640,
"likes": 18,
"imageWidth": 3863,
"id": 3708739,
"user_id": 10248012,
"views": 2696,
"comments": 2,
"pageURL": "https://pixabay.com/photos/sky-dark-night-moon-moonlight-3708739/",
"imageHeight": 2170,
"webformatURL": "https://pixabay.com/get/55e7d54b4d51a514f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_640.jpg",
"type": "photo",
"previewHeight": 84,
"tags": "sky, dark, night",
"downloads": 1036,
"user": "Sritam_kumar_Sethy",
"favorites": 25,
"imageSize": 2027189,
"previewWidth": 150,
"userImageURL": "https://cdn.pixabay.com/user/2019/09/28/19-27-53-806_250x250.jpeg",
"previewURL": "https://cdn.pixabay.com/photo/2018/09/28/08/37/sky-3708739_150.jpg"
},
{
"largeImageURL": "https://pixabay.com/get/57e2dc4a4c5aaf14f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_1280.jpg",
"webformatHeight": 360,
"webformatWidth": 640,
"likes": 14,
"imageWidth": 4608,
"id": 1299683,
"user_id": 1151065,
"views": 2932,
"comments": 3,
"pageURL": "https://pixabay.com/photos/panda-z-panda-toy-children-child-1299683/",
"imageHeight": 2592,
"webformatURL": "https://pixabay.com/get/57e2dc4a4c5aaf14f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_640.jpg",
"type": "photo",
"previewHeight": 84,
"tags": "panda-z, panda, toy",
"downloads": 1067,
"user": "vinsky2002",
"favorites": 7,
"imageSize": 1745060,
"previewWidth": 150,
"userImageURL": "https://cdn.pixabay.com/user/2019/05/15/02-40-06-911_250x250.png",
"previewURL": "https://cdn.pixabay.com/photo/2016/04/01/09/56/panda-z-1299683_150.jpg"
},
],
"total": 588
}
The API I'm fetching returns json value like this. I wrote the following code to extract the data, it works if I code it like this and the widget builds, but with this error before the widget loads
Future getJsonData() async{
var response;
for(int i=1; i<=5; i++){
response = await http.get(
Uri.encodeFull(widget.api+i.toString()),
headers: {"Accept": "application/json"}
);
setState(() {
var toJsonData = json.decode(response.body);
item.addAll(toJsonData['hits']);
});
}
print(response.body);
return item;
}
error image
But when I use FutureBuilder method, it fails to create the widget. It would be helpful if someone can tell a possibly working FutureBuilder method for this json data.

Set json response get_Infographs value in recycler view of this data

{
"count": 7,
"next": null,
"previous": null,
"results": [
{
"get_Infographs": [
{
"viewCount": 17,
"likecount": 1,
"unlikeCount": 1,
"shareCount": 0,
"i_id": 532,
"p_id": 532,
"name": "Yoga happiness educational s",
"description": "Yoga happiness educational spiritual journey lifestyle health ",
"c_id": 1,
"status_id": 1,
"source_id": 316,
"date_created": null,
"parentinfograph": "Language english grammar apo",
"internal_url": "http://167.99.86.81:8000/media/DQ532.png",
"external_url": "http://yogadork.com/wp-content/uploads/2015/01/2015-yoga-resolutions-infographic.png",
"mt_id": 3,
"mtcode": "Education",
"topics": [
{
"topic_code": "#EDUCATION"
},
{
"topic_code": "#HEALTH"
}
],
"geoPolitical": [
{
"gp_code": "Default / All GPIDs"
}
],
"entity": [],
"customMetaTags": [
{
"metatag": "#"
}
]
}
]
}
]
}
Read customMetaTags it as ArrayList and then you can pass it using bundle
bundle.putSerializable("some_string", arrayList)

Not able to load nested Json Array

My Json Value is
{"candidateName":"Check","success":true,"candidateWorkExperience":"[{\"candidateWorkExperienceId\":7486,\"candidateBiographyRef\":{\"primaryKey\":98567,\"percent\":0.0,\"specialization\":0,\"rating\":0,\"description\":\"Check\",\"properties\":{},\"secondaryKey\":0,\"start\":0,\"limit\":0,\"noOfStud\":0,\"typingTest\":false,\"isPostGrad\":false},\"designation\":\"Check\",\"company\":\"Check\",\"startDate\":\"Jan 1, 2012\",\"endDate\":\"Jan 1, 2014\",\"workTypeMasterRef\":{\"primaryKey\":2,\"percent\":0.0,\"specialization\":0,\"rating\":0,\"description\":\"Permanent\",\"properties\":{},\"secondaryKey\":0,\"start\":0,\"limit\":0,\"noOfStud\":0,\"typingTest\":false,\"isPostGrad\":false},\"achivements\":\"Check\",\"candidateRecordStatus\":\"EXISTING\",\"currentCompany\":\"false\"}]"}
I am trying to pass the same by
JSONArray jarray = jsono.getJSONArray("candidateWorkExperience");
But I am getting org.json.JSON.typeMismatch exception.
Can any one suggest how to load the json data into the array.
Your problem is that you put "[]" which js is interpreting as a string not an array.
The arrays are enclosed by [] with no ".
Try the next json and let me know if this fix your problem:
{
"candidateName": "Check",
"success": true,
"candidateWorkExperience": [
{
"candidateWorkExperienceId": 7486,
"candidateBiographyRef": {
"primaryKey": 98567,
"percent": 0.0,
"specialization": 0,
"rating": 0,
"description": "Check",
"properties": {
},
"secondaryKey": 0,
"start": 0,
"limit": 0,
"noOfStud": 0,
"typingTest": false,
"isPostGrad": false
},
"designation": "Check",
"company": "Check",
"startDate": "Jan1, 2012",
"endDate": "Jan1, 2014",
"workTypeMasterRef": {
"primaryKey": 2,
"percent": 0.0,
"specialization": 0,
"rating": 0,
"description": "Permanent",
"properties": {
},
"secondaryKey": 0,
"start": 0,
"limit": 0,
"noOfStud": 0,
"typingTest": false,
"isPostGrad": false
},
"achivements": "Check",
"candidateRecordStatus": "EXISTING",
"currentCompany": "false"
}
]
}
Your JSON is not correct, your "candidateWorkExperience" parameter is not a JSONArray but a JSONString.
Paste your JSON here
And you will see how you JSON is parsed.
Cheers !

Categories

Resources