How to get Image URL based on name - android

My JSON contains only name of the food categories, Looks like below
[
{
"cu_id": "2",
"cuisine_type": "American"
},
{
"cu_id": "24",
"cuisine_type": "Sandwich"
},
{
"cu_id": "17",
"cuisine_type": "Seafood"
},
{
"cu_id": "29",
"cuisine_type": "Sports Bar"
},
{
"cu_id": "5",
"cuisine_type": "Steak"
},
{
"cu_id": "20",
"cuisine_type": "Sushi"
}
]
I want to get default image automatically from Internet by searching 'cuisine_type', How to do that?

You can use pixabay api it's a free RESTful api for searching and retrieving free images and videos released on Pixabay under Creative Commons CC0.
Sending a GET request like this :
https://pixabay.com/api/?key={ KEY }&q=query&image_type=photo
where :
Key : the api key you get after creating an account
q : the queried object you need to look for
image_type : what kind of data you need (picture, video)
returned data will be something like this :
{
"total": 4692,
"totalHits": 500,
"hits": [
{
"id": 195893,
"pageURL": "https://pixabay.com/en/blossom-bloom-flower-yellow-close-195893/",
"type": "photo",
"tags": "blossom, bloom, flower",
"previewURL": "https://static.pixabay.com/photo/2013/10/15/09/12/flower-195893_150.jpg"
"previewWidth": 150,
"previewHeight": 84,
"webformatURL": "https://pixabay.com/get/35bbf209db8dc9f2fa36746403097ae226b796b9e13e39d2_640.jpg",
"webformatWidth": 640,
"webformatHeight": 360,
"imageWidth": 4000,
"imageHeight": 2250,
"imageSize": 4731420,
"views": 7671,
"downloads": 6439,
"favorites": 1,
"likes": 5,
"comments": 2,
"user_id": 48777,
"user": "Josch13",
"userImageURL": "https://static.pixabay.com/user/2013/11/05/02-10-23-764_250x250.jpg",
},
{
"id": 14724,
...
},
...
]
}
for more detail visit this link

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

Dynamic JSON parsing

I am a newbie to android. I am stuck at JSON parsing.
Following is my JSON, in this, the type of menu categories will be dynamic, and I want to arrange this JSON in sectioned recycler view.
Please help me out.
This is my JSON
{
"menu_items": {
"Plat": [{
"menu_name": "jambon à l'os",
"menu_cost": "18.00",
"menu_count": "1",
"category": "Plat",
"menu_tax": "5.50",
"code": "€"
},
{
"menu_name": "tacos",
"menu_cost": "6.00",
"menu_count": "1",
"category": "Plat",
"menu_tax": "5.50",
"code": "€"
}
],
"Entrée": [{
"menu_name": "avocat",
"menu_cost": "7.50",
"menu_count": "1",
"category": "Entrée",
"menu_tax": "5.50",
"code": "€"
}]
},
"order_data": {
"order_id": "278",
"order_number": "REF-5d5e6b86",
"created_at": "1566468998",
"instructions": "",
"fullname": "Rohan Chitnis",
"phonenumber": "7610771871",
"useraddress": "Brest, France",
"image_path": "https://restau-at-home.bzh/uploads/attachments/Jellyfish_2019_08_21_10_49_21.jpg",
"wp": "pune",
"code": "€",
"promocode": null,
"promo_value": null,
"promo_type": null
},
"order_details": {
"total_items": "3",
"order_total": "31.50"
},
"promo_applied": "0",
"error": {
"code": "13",
"status": "200",
"message": "Ok"
}
}
Check this out. Create a generic response class. Extend your classes from that class. To do this properly you can look at Java Generics and Inheritance topics if you are using Java. And then search about Sectioned RecyclerView in Android. Here is a library for this.

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

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