How to obtain/parse JSONObject data with GSON - android

I am trying to use retrofit with android, which is kind of forcing me to use gson.
I am trying to get the data from the payload["response"]["docs"][0]["web_url"] in my code.
Issue : the key getResponse(), from below model class, always gives me {}. Whats the right way to handle this in android? Also note, the getCopyright() & getStatus() are working fine.
I am also providing the sample payload.
I have the model class like this :
public class ArticleSearchResponseModel {
#SerializedName("status") private String status;
#SerializedName("copyright") private String copyright;
#SerializedName("response") private JSONObject response;
public String getStatus() {
return status;
}
public String getCopyright() {
return copyright;
}
public JSONObject getResponse() {
return response;
}
public void setStatus(String status) {
this.status = status;
}
public void setCopyright(String copyright) {
this.copyright = copyright;
}
public void setResponse(JSONObject response) {
this.response = response;
}
}
My payload :
{
"response": {
"meta": {
"hits": 15987906,
"time": 108,
"offset": 0
},
"docs": [
{
"web_url": "http://www.nytimes.com/2016/11/13/insider/gender-issues-in-sharp-focus-at-the-times.html",
"snippet": "Susan Chira, senior correspondent for gender issues, and Susan Dominus, a Times Magazine writer, talk about men, women and coverage of their changing roles.",
"lead_paragraph": "Susan Chira, senior correspondent for gender issues, and Susan Dominus, a Times Magazine writer, talk about men, women and coverage of their changing roles.",
"abstract": null,
"print_page": null,
"blog": [],
"source": "The New York Times",
"multimedia": [
{
"width": 190,
"url": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-thumbWide.jpg",
"height": 126,
"subtype": "wide",
"legacy": {
"wide": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-thumbWide.jpg",
"wideheight": "126",
"widewidth": "190"
},
"type": "image"
},
{
"width": 600,
"url": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-articleLarge.jpg",
"height": 400,
"subtype": "xlarge",
"legacy": {
"xlargewidth": "600",
"xlarge": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-articleLarge.jpg",
"xlargeheight": "400"
},
"type": "image"
},
{
"width": 75,
"url": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-thumbStandard.jpg",
"height": 75,
"subtype": "thumbnail",
"legacy": {
"thumbnailheight": "75",
"thumbnail": "images/2016/10/14/insider/14-Insider-SUSANIMAGE/14-Insider-SUSANIMAGE-thumbStandard.jpg",
"thumbnailwidth": "75"
},
"type": "image"
}
],
"headline": {
"main": "Gender Issues in Sharp Focus at The Times",
"content_kicker": "Insider Podcasts",
"kicker": "Insider Podcast"
},
"keywords": [
{
"rank": "1",
"is_major": "N",
"name": "subject",
"value": "Presidential Election of 2016"
},
{
"rank": "2",
"is_major": "N",
"name": "subject",
"value": "Women and Girls"
},
{
"rank": "3",
"is_major": "N",
"name": "subject",
"value": "Gender"
},
{
"rank": "4",
"is_major": "N",
"name": "subject",
"value": "Men and Boys"
},
{
"rank": "5",
"is_major": "N",
"name": "persons",
"value": "Trump, Donald J"
}
],
"pub_date": "2016-11-13T00:00:00Z",
"document_type": "article",
"news_desk": "Insider",
"section_name": "Times Insider",
"subsection_name": null,
"byline": {
"person": [
{
"organization": "",
"role": "reported",
"firstname": "Susan",
"rank": 1,
"lastname": "LEHMAN"
}
],
"original": "By SUSAN LEHMAN"
},
"type_of_material": "News",
"_id": "58011d9b253f0a0e44966174",
"word_count": null,
"slideshow_credits": null
},
{
"web_url": "http://www.nytimes.com/2016/10/25/realestate/doors-of-manhattan.html",
"snippet": "Even though they stand between us and the chaos of the city, the borough’s distinctive doors are easy to overlook.",
"lead_paragraph": "Even though they stand between us and the chaos of the city, the borough’s distinctive doors are easy to overlook.",
"abstract": null,
"print_page": null,
"blog": [],
"source": "The New York Times",
"multimedia": [],
"headline": {
"main": "Doors of Manhattan",
"content_kicker": "Voyeur",
"kicker": "Voyeur"
},
"keywords": [
{
"rank": "1",
"is_major": "N",
"name": "subject",
"value": "Real Estate and Housing (Residential)"
},
{
"rank": "2",
"is_major": "N",
"name": "subject",
"value": "Architecture"
},
{
"rank": "3",
"is_major": "N",
"name": "glocations",
"value": "Upper West Side (Manhattan, NY)"
},
{
"rank": "4",
"is_major": "N",
"name": "glocations",
"value": "Upper East Side (Manhattan, NY)"
}
],
"pub_date": "2016-10-25T00:00:00Z",
"document_type": "article",
"news_desk": "RealEstate",
"section_name": "Real Estate",
"subsection_name": null,
"byline": {
"person": [
{
"firstname": "Photographs",
"middlename": "George",
"lastname": "ETHEREDGE",
"rank": 1,
"role": "reported",
"organization": ""
}
],
"original": "Photographs by GEORGE ETHEREDGE for THE NEW YORK TIMES"
},
"type_of_material": "News",
"_id": "57e94175253f0a6bb40cbffc",
"word_count": null,
"slideshow_credits": null
},
{
"web_url": "http://cooking.nytimes.com/recipes/1018356-celery-toasts",
"snippet": "This was the first recipe that the chef and writer Gabrielle Hamilton brought to The Times as an Eat columnist for the Sunday magazine in 2016, a snack-tray-sandwich version of a celery-and-fennel salad served at her restaurant, Prune, in the East...",
"lead_paragraph": "This was the first recipe that the chef and writer Gabrielle Hamilton brought to The Times as an Eat columnist for the Sunday magazine in 2016, a snack-tray-sandwich version of a celery-and-fennel salad served at her restaurant, Prune, in the East Village. It calls for thick, white toasted Pullman bread spread wall to wall with unsalted butter, with slices of blue cheese neatly laid on top, below a mound of shaved celery and thinly sliced scallions dressed in garlic, olive oil, lemon juice and salt, and the whole shebang dusted in ground black pepper before being cut in halves or quarters. \"The ingredients come from the grocery store,\" she wrote in her column. \"These toasts are not expensive or intimidating, but they are outstanding.\"",
"abstract": null,
"print_page": null,
"blog": [],
"source": "du_recipe",
"multimedia": [
{
"credit": "Davide Luciano for The New York Times. Food stylist: Michelle Gatton. Prop stylist: Alex Brannian.",
"url": "images/2016/10/23/magazine/23eat/23eat-thumbStandard-v2.jpg",
"rank": "1",
"height": 75,
"subtype": "thumbnail",
"legacy": [],
"caption": "Celery Toasts",
"type": "image",
"width": 75
},
{
"credit": "Davide Luciano for The New York Times. Food stylist: Michelle Gatton. Prop stylist: Alex Brannian.",
"url": "images/2016/10/23/magazine/23eat/23mag-23eat-t_CA1-articleLarge.jpg",
"rank": "1",
"height": 400,
"subtype": "large",
"legacy": [],
"caption": "Celery Toasts",
"type": "image",
"width": 600
}
],
"headline": {
"main": "A Snack Tray to Gather the Family Around",
"name": "Celery Toasts"
},
"keywords": [],
"pub_date": "2016-10-23T00:00:00Z",
"document_type": "recipe",
"news_desk": null,
"section_name": null,
"subsection_name": null,
"byline": {
"person": [
{
"organization": "",
"role": "reported",
"firstname": "Gabrielle",
"rank": 1,
"lastname": "Hamilton"
}
],
"original": "Gabrielle Hamilton"
},
"type_of_material": "Recipe",
"_id": "580696f1253f0a7d028b0864",
"word_count": "101",
"slideshow_credits": null
},
{
"web_url": "http://www.nytimes.com/2016/10/23/books/review/the-story-behind-this-weeks-best-sellers.html",
"snippet": "Ruth Bader Ginsburg, whose anthology “My Own Words” is No. 12 in hardcover nonfiction, has something in common with Notorious B.I.G.",
"lead_paragraph": "Ruth Bader Ginsburg, whose anthology “My Own Words” is No. 12 in hardcover nonfiction, has something in common with Notorious B.I.G.",
"abstract": null,
"print_page": "30",
"blog": [],
"source": "The New York Times",
"multimedia": [
{
"width": 190,
"url": "images/2016/10/23/books/review/23inside-list/23inside-list-thumbWide.jpg",
"height": 126,
"subtype": "wide",
"legacy": {
"wide": "images/2016/10/23/books/review/23inside-list/23inside-list-thumbWide.jpg",
"wideheight": "126",
"widewidth": "190"
},
"type": "image"
},
{
"width": 75,
"url": "images/2016/10/23/books/review/23inside-list/23inside-list-thumbStandard.jpg",
"height": 75,
"subtype": "thumbnail",
"legacy": {
"thumbnailheight": "75",
"thumbnail": "images/2016/10/23/books/review/23inside-list/23inside-list-thumbStandard.jpg",
"thumbnailwidth": "75"
},
"type": "image"
}
],
"headline": {
"main": "The Story Behind This Week’s Best Sellers"
},
"keywords": [
{
"rank": "1",
"is_major": "N",
"name": "subject",
"value": "Books and Literature"
},
{
"rank": "2",
"is_major": "N",
"name": "persons",
"value": "Ginsburg, Ruth Bader"
}
],
"pub_date": "2016-10-23T00:00:00Z",
"document_type": "article",
"news_desk": "BookReview",
"section_name": "Books",
"subsection_name": "Book Review",
"byline": {
"person": [
{
"organization": "",
"role": "reported",
"firstname": "Gregory",
"rank": 1,
"lastname": "COWLES"
}
],
"original": "By GREGORY COWLES"
},
"type_of_material": "News",
"_id": "5800f337253f0a7985625011",
"word_count": null,
"slideshow_credits": null
},
{
"web_url": "http://www.nytimes.com/2016/10/23/books/review/ngugi-wa-thiongo-birth-of-a-dream-weaver.html",
"snippet": "A Kenyan considers his early years as a student and writer.",
"lead_paragraph": "A Kenyan considers his early years as a student and writer.",
"abstract": null,
"print_page": "25",
"blog": [],
"source": "The New York Times",
"multimedia": [
{
"width": 190,
"url": "images/2016/10/23/books/review/23Wrong/23Wrong-thumbWide.jpg",
"height": 126,
"subtype": "wide",
"legacy": {
"wide": "images/2016/10/23/books/review/23Wrong/23Wrong-thumbWide.jpg",
"wideheight": "126",
"widewidth": "190"
},
"type": "image"
},
{
"width": 600,
"url": "images/2016/10/23/books/review/23Wrong/23Wrong-articleLarge.jpg",
"height": 400,
"subtype": "xlarge",
"legacy": {
"xlargewidth": "600",
"xlarge": "images/2016/10/23/books/review/23Wrong/23Wrong-articleLarge.jpg",
"xlargeheight": "400"
},
"type": "image"
},
{
"width": 75,
"url": "images/2016/10/23/books/review/23Wrong/23Wrong-thumbStandard.jpg",
"height": 75,
"subtype": "thumbnail",
"legacy": {
"thumbnailheight": "75",
"thumbnail": "images/2016/10/23/books/review/23Wrong/23Wrong-thumbStandard.jpg",
"thumbnailwidth": "75"
},
"type": "image"
}
],
"headline": {
"main": "Ngugi wa Thiong’o on Starting Out as a Writer",
"content_kicker": "Nonfiction",
"kicker": "Nonfiction",
"print_headline": "A Long Look Back"
},
"keywords": [
{
"rank": "2",
"is_major": "N",
"name": "persons",
"value": "Thiong'o, Ngugi wa"
},
{
"rank": "3",
"is_major": "N",
"name": "subject",
"value": "Books and Literature"
},
{
"rank": "4",
"is_major": "N",
"name": "subject",
"value": "Writing and Writers"
}
],
"pub_date": "2016-10-23T00:00:00Z",
"document_type": "article",
"news_desk": "BookReview",
"section_name": "Books",
"subsection_name": "Book Review",
"byline": {
"person": [
{
"organization": "",
"role": "reported",
"firstname": "Michela",
"rank": 1,
"lastname": "WRONG"
}
],
"original": "By MICHELA WRONG"
},
"type_of_material": "Review",
"_id": "57ff4d1d253f0a6b9720abd2",
"word_count": null,
"slideshow_credits": null
},
{
"web_url": "http://cooking.nytimes.com/recipes/1018347-lemon-drizzle-cake",
"snippet": "This light and moist lemon poundcake has a crunchy sugar glaze that crystallizes on top, giving a contrasting texture to the soft crumb underneath. It’s an easy-to-make, crowd-pleasing cake that’s excellent on its own but takes well to...",
"lead_paragraph": "This light and moist lemon poundcake has a crunchy sugar glaze that crystallizes on top, giving a contrasting texture to the soft crumb underneath. It’s an easy-to-make, crowd-pleasing cake that’s excellent on its own but takes well to embellishments. A scoop of ice cream or sorbet, fruit compote and-or lemon curd are all wonderful alongside.",
"abstract": null,
"print_page": null,
"blog": [],
"source": "du_recipe",
"multimedia": [
{
"credit": "Andrew Scrivani for The New York Times",
"url": "images/2016/10/19/dining/19BAKEOFF2/19BAKEOFF2-thumbStandard.jpg",
"rank": "1",
"height": 75,
"subtype": "thumbnail",
"legacy": [],
"type": "image",
"width": 75
},
{
"credit": "Andrew Scrivani for The New York Times",
"url": "images/2016/10/19/dining/19BAKEOFF2/19BAKEOFF2-articleLarge.jpg",
"rank": "1",
"height": 400,
"subtype": "large",
"legacy": [],
"type": "image",
"width": 600
}
],
"headline": {
"main": "‘The Great British Bake Off’ Changes the Way the British Bake",
"name": "Lemon Drizzle Cake"
},
"keywords": [],
"pub_date": "2016-10-19T00:00:00Z",
"document_type": "recipe",
"news_desk": null,
"section_name": null,
"subsection_name": null,
"byline": {
"person": [
{
"organization": "",
"role": "reported",
"firstname": "Melissa",
"rank": 1,
"lastname": "Clark"
}
],
"original": "Melissa Clark"
},
"type_of_material": "Recipe",
"_id": "58066d7c253f0a7d028b07d0",
"word_count": "250",
"slideshow_credits": null
}
]
},
"status": "OK",
"copyright": "Copyright (c) 2013 The New York Times Company. All Rights Reserved."
}

Before start, I'm not familiar with retrofit.
If you want serialize/unserialize json to pojo, you better make pojo object.
class Response {
Meta meta;
List<Docs> docs;
}
class Docs {
String webUrl;
Mutimedia multimedia;
...
}
The issue the key getResponse(), from below model class, always gives me {} happens because gson cannot determine proper object to convert json to plain old java object.

In order to make your Response model class use http://www.jsonschema2pojo.org/.
Making POJO simple

you no need to make exact structure in model
you can go ahead with your code just need small change
Use
#SerializedName("response") private JsonObject response;
instead of
#SerializedName("response") private JSONObject response;
and in the import
import com.google.gson.JsonObject;

Related

Android Room Write Custom Nested Objects

Okay so I've successfully sent GET request to a server using Retrofit library, and parsed that data in my app. Now I want to store that data(custom model classes which I have created) in ROOM database, however I have a lot of model classes. So what's the best way to write that custom objects in database, how should I write type converters for this? Check my network response to understand how complex the data is:
{
"recipes": [
{
"vegetarian": false,
"vegan": false,
"glutenFree": true,
"dairyFree": false,
"veryHealthy": false,
"cheap": false,
"veryPopular": false,
"sustainable": false,
"weightWatcherSmartPoints": 9,
"gaps": "no",
"lowFodmap": false,
"aggregateLikes": 27,
"spoonacularScore": 27,
"healthScore": 2,
"creditsText": "Foodista.com – The Cooking Encyclopedia Everyone Can Edit",
"license": "CC BY 3.0",
"sourceName": "Foodista",
"pricePerServing": 168.58,
"extendedIngredients": [
{
"id": 1001,
"aisle": "Milk, Eggs, Other Dairy",
"image": "butter-sliced.jpg",
"consistency": "solid",
"name": "butter",
"original": "1 tablespoon butter",
"originalString": "1 tablespoon butter",
"originalName": "butter",
"amount": 1,
"unit": "tablespoon",
"meta": [],
"metaInformation": [],
"measures": {
"us": {
"amount": 1,
"unitShort": "Tbsp",
"unitLong": "Tbsp"
},
"metric": {
"amount": 1,
"unitShort": "Tbsp",
"unitLong": "Tbsp"
}
}
},
{
"id": 10011268,
"aisle": "Produce;Ethnic Foods",
"image": "dried-porcini-mushrooms.png",
"consistency": "solid",
"name": "dried porcini mushrooms",
"original": "1 cup porcini mushrooms (dried or fresh)",
"originalString": "1 cup porcini mushrooms (dried or fresh)",
"originalName": "porcini mushrooms (dried or fresh)",
"amount": 1,
"unit": "cup",
"meta": [
"fresh",
"()"
],
"metaInformation": [
"fresh",
"()"
],
"measures": {
"us": {
"amount": 1,
"unitShort": "cup",
"unitLong": "cup"
},
"metric": {
"amount": 236.588,
"unitShort": "ml",
"unitLong": "milliliters"
}
}
},
{
"id": 1053,
"aisle": "Milk, Eggs, Other Dairy",
"image": "fluid-cream.jpg",
"consistency": "liquid",
"name": "heavy cream",
"original": "1/2 cup heavy cream (or until desired consistency)",
"originalString": "1/2 cup heavy cream (or until desired consistency)",
"originalName": "heavy cream (or until desired consistency)",
"amount": 0.5,
"unit": "cup",
"meta": [
"(or until desired consistency)"
],
"metaInformation": [
"(or until desired consistency)"
],
"measures": {
"us": {
"amount": 0.5,
"unitShort": "cups",
"unitLong": "cups"
},
"metric": {
"amount": 118.294,
"unitShort": "ml",
"unitLong": "milliliters"
}
}
},
{
"id": 1033,
"aisle": "Cheese",
"image": "parmesan.jpg",
"consistency": "solid",
"name": "parmesan cheese",
"original": "1 cup grated Parmesan cheese (or more to taste)",
"originalString": "1 cup grated Parmesan cheese (or more to taste)",
"originalName": "grated Parmesan cheese (or more to taste)",
"amount": 1,
"unit": "cup",
"meta": [
"grated",
"to taste",
"( )"
],
"metaInformation": [
"grated",
"to taste",
"( )"
],
"measures": {
"us": {
"amount": 1,
"unitShort": "cup",
"unitLong": "cup"
},
"metric": {
"amount": 236.588,
"unitShort": "ml",
"unitLong": "milliliters"
}
}
},
{
"id": 10035137,
"aisle": "Pasta and Rice;Ethnic Foods;Baking",
"image": "cornmeal.png",
"consistency": "solid",
"name": "polenta",
"original": "1 cup polenta",
"originalString": "1 cup polenta",
"originalName": "polenta",
"amount": 1,
"unit": "cup",
"meta": [],
"metaInformation": [],
"measures": {
"us": {
"amount": 1,
"unitShort": "cup",
"unitLong": "cup"
},
"metric": {
"amount": 236.588,
"unitShort": "ml",
"unitLong": "milliliters"
}
}
},
{
"id": 2047,
"aisle": "Spices and Seasonings",
"image": "salt.jpg",
"consistency": "solid",
"name": "salt",
"original": "Salt to taste",
"originalString": "Salt to taste",
"originalName": "Salt to taste",
"amount": 6,
"unit": "servings",
"meta": [
"to taste"
],
"metaInformation": [
"to taste"
],
"measures": {
"us": {
"amount": 6,
"unitShort": "servings",
"unitLong": "servings"
},
"metric": {
"amount": 6,
"unitShort": "servings",
"unitLong": "servings"
}
}
},
{
"id": 14412,
"aisle": "Beverages",
"image": "water.png",
"consistency": "liquid",
"name": "water",
"original": "4 cups water",
"originalString": "4 cups water",
"originalName": "water",
"amount": 4,
"unit": "cups",
"meta": [],
"metaInformation": [],
"measures": {
"us": {
"amount": 4,
"unitShort": "cups",
"unitLong": "cups"
},
"metric": {
"amount": 946.352,
"unitShort": "ml",
"unitLong": "milliliters"
}
}
}
],
"id": 640677,
"title": "Creamy Porcini Mushroom Polenta",
"readyInMinutes": 45,
"servings": 6,
"sourceUrl": "http://www.foodista.com/recipe/F38KZJ88/creamy-porcini-mushroom-polenta",
"image": "https://spoonacular.com/recipeImages/640677-556x370.jpg",
"imageType": "jpg",
"summary": "Creamy Porcini Mushroom Polentan is a <b>gluten free</b> side dish. This recipe makes 6 servings with <b>263 calories</b>, <b>9g of protein</b>, and <b>14g of fat</b> each. For <b>$1.69 per serving</b>, this recipe <b>covers 7%</b> of your daily requirements of vitamins and minerals. Head to the store and pick up water, porcini mushrooms, heavy cream, and a few other things to make it today. 27 people have made this recipe and would make it again. From preparation to the plate, this recipe takes approximately <b>45 minutes</b>. All things considered, we decided this recipe <b>deserves a spoonacular score of 30%</b>. This score is not so super. Try Wild Mushroom Polenta with Porcini Sauce, Creamy polenta & mushroom ragout, and Polenta with Creamy Mushroom Sauce for similar recipes.",
"cuisines": [],
"dishTypes": [
"side dish"
],
"diets": [
"gluten free"
],
"occasions": [],
"winePairing": {},
"instructions": "<ol><li>In a large heavy bottomed saucepan, season water with salt and bring to a boil. Quickly whisk in the polenta until fully incorporated.</li><li>Lower the heat to a simmer, add the butter and porcini and allow the polenta to cook, stirring occasionally for about 30 minutes.</li><li>Finish by stirring in the cream and Parmesan cheese. If necessary, add salt to taste.</li></ol>",
"analyzedInstructions": [
{
"name": "",
"steps": [
{
"number": 1,
"step": "In a large heavy bottomed saucepan, season water with salt and bring to a boil. Quickly whisk in the polenta until fully incorporated.Lower the heat to a simmer, add the butter and porcini and allow the polenta to cook, stirring occasionally for about 30 minutes.Finish by stirring in the cream and Parmesan cheese. If necessary, add salt to taste.",
"ingredients": [
{
"id": 1033,
"name": "parmesan",
"localizedName": "parmesan",
"image": "parmesan.jpg"
},
{
"id": 10035137,
"name": "polenta",
"localizedName": "polenta",
"image": "cornmeal.png"
},
{
"id": 1001,
"name": "butter",
"localizedName": "butter",
"image": "butter-sliced.jpg"
},
{
"id": 1053,
"name": "cream",
"localizedName": "cream",
"image": "fluid-cream.jpg"
},
{
"id": 14412,
"name": "water",
"localizedName": "water",
"image": "water.png"
},
{
"id": 2047,
"name": "salt",
"localizedName": "salt",
"image": "salt.jpg"
}
],
"equipment": [
{
"id": 404669,
"name": "sauce pan",
"localizedName": "sauce pan",
"image": "sauce-pan.jpg"
},
{
"id": 404661,
"name": "whisk",
"localizedName": "whisk",
"image": "whisk.png"
}
],
"length": {
"number": 30,
"unit": "minutes"
}
}
]
}
],
"originalId": null,
"spoonacularSourceUrl": "https://spoonacular.com/creamy-porcini-mushroom-polenta-640677"
}
]
}
Although there's no best way of handling this, besides having to create converters models for each of these fields, it would be wise to limit the amount of data you're saving to only the relevant information that you're going to need and then to use custom converters to convert your data automatically.
As requested by OP in the comments, here's a basic example of how you can add a type converter for a list/array (making use of Gson):
extension methods:
fun <T> String.fromGson(classOfT: Class<T>) = Gson().fromJson(this, classOfT)
fun Any.toGson() = Gson().toJson(this)
converter:
class MyConverter {
#TypeConverter
fun toListOfObjects(value: String?): List<Foo> {
if (value.isNullOrEmpty()) {
return emptyList()
}
val jsonArray = value.fromGson(JsonArray::class.java)
return jsonArray.map { jsonArray -> jsonArray.fromGson(Foo::class.java) }
}
#TypeConverter
fun toString(value: List<Foo>?) = value?.toGson()
}
similar question/answer

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.

missing fields in profile from LinkedIn Android SDK

I'm using the LinkedIn Android SDK downloaded from here (version 1.1.4) and have set up authentication already like this:
liSessionManager.init(activity,
Scope.build(Scope.R_BASICPROFILE, Scope.R_EMAILADDRESS), authListener);
and everything works fine, but when I want to get the profile data, returned json is missing fields like summary in job positions and general summary for the given profile.
The request is made like this:
apiHelper.getRequest(activity, GET_LINKED_IN_PROFILE_URL, apiListener);
where GET_LINKED_IN_PROFILE_URL is https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,formatted-name,headline,location,industry,current-share,num-connections,num-connections-capped,summary,specialties,positions,phone-numbers,public-profile-url,picture-url,picture-urls::(original))?format=json.
The response is as follows:
{
"currentShare": {
"author": {
"firstName": "Name",
"id": "***",
"lastName": "Surname"
},
"comment": "I've made my LinkedIn profile visual! Check it out and get yours!",
"content": {
"description": "some descr",
"eyebrowUrl": "http://url.com/me/7827748?m_in&user_id=***",
"resolvedUrl": "http://url.com/me/7827748?m_in&user_id=***",
"shortenedUrl": "http://linkedin.in/url",
"submittedImageUrl": "https://www.url.com/logo-share.png",
"submittedUrl": "https://www.url.com/me/***?m_in&user_id=***",
"thumbnailUrl": "https://media.licdn.com/media-proxy/ext?w=80&h=100&f=&hash=usON2E%2F4FqEGtIT5dsadsaaD&ora=1%2CaFBCTXdkRmpGL2lvQUFBPQ%2CxAVta5g-0R65wFUbzx0776dsaulz-50hKCpDZEXX8HmW3uZHfgasdapuGJ-n08ARIZ3Bex1svPLf5AmX_Us3r",
"title": "title"
},
"id": "s1291360434",
"source": {
"application": {"name": "app"},
"serviceProvider": {"name": "LINKEDIN"}
},
"timestamp": 1346884888000,
"visibility": {"code": "anyone"}
},
"emailAddress": "***#gmail.com",
"firstName": "Name",
"formattedName": "Name Surname",
"headline": "Job position",
"id": "***",
"industry": "Computer Software",
"lastName": "Surname",
"location": {
"country": {"code": "pl"},
"name": "Wroclaw, Lower Silesian District, Poland"
},
"numConnections": 229,
"numConnectionsCapped": false,
"pictureUrl": "https://media.licdn.com/mpr/mprx/photourl",
"pictureUrls": {
"_total": 1,
"values": ["https://media.licdn.com/mpr/mprx/photourl"]
},
"positions": {
"_total": 1,
"values": [{
"company": {
"id": 2795963,
"industry": "Information Technology & Services",
"name": "Company 1",
"size": "11-50",
"type": "Privately Held"
},
"id": ***,
"isCurrent": true,
"location": {
"country": {
"code": "pl",
"name": "Poland"
},
"name": "Wroclaw, Lower Silesian District, Poland"
},
"startDate": {
"month": 1,
"year": 2016
},
"title": "Job position"
}]
},
"publicProfileUrl": "https://www.linkedin.com/in/username/41219"
}
When I use the same url in the API Console at https://apigee.com/console/linkedin, the response looks like this:
{
"currentShare": {
"author": {
"firstName": "Name",
"id": "***",
"lastName": "Surname"
},
"comment": "I've made my LinkedIn profile visual! Check it out and get yours!",
"content": {
"description": "some descr",
"eyebrowUrl": "http://url.com/me/7827748?m_in&user_id=***",
"resolvedUrl": "http://url.com/me/7827748?m_in&user_id=***",
"shortenedUrl": "http://linkedin.com/shortened",
"submittedImageUrl": "https://www.url.com/logo-share.png",
"submittedUrl": "https://www.url.com/me/***?m_in&user_id=***",
"thumbnailUrl": "https://media.licdn.com/media-proxy/ext?w=80&h=100&f=&hash=usON2E%2F4FqEGtIT5dsadsaaD&ora=1%2CaFBCTXdkRmpGL2lvQUFBPQ%2CxAVta5g-0R65wFUbzx0776dsaulz-50hKCpDZEXX8HmW3uZHfgasdapuGJ-n08ARIZ3Bex1svPLf5AmX_Us3r",
"title": "title"
},
"id": "s1291360434",
"source": {
"application": {
"name": "app"
},
"serviceProvider": {
"name": "LINKEDIN"
}
},
"timestamp": 1346884888000,
"visibility": {
"code": "anyone"
}
},
"emailAddress": "***#gmail.com",
"firstName": "Name",
"formattedName": "Name Surname",
"headline": "Job position",
"id": "***",
"industry": "Computer Software",
"lastName": "Surname",
"location": {
"country": {
"code": "pl"
},
"name": "Wroclaw, Lower Silesian District, Poland"
},
"numConnections": 229,
"numConnectionsCapped": false,
"pictureUrl": "https://media.licdn.com/mpr/mprx/photourl",
"pictureUrls": {
"_total": 1,
"values": [
"https://media.licdn.com/mpr/mprx/photourl"
]
},
"positions": {
"_total": 1,
"values": [
{
"company": {
"id": 2795963,
"industry": "Information Technology & Services",
"name": "Company 1",
"size": "11-50",
"type": "Privately Held"
},
"id": 22222,
"isCurrent": true,
"location": {
"country": {
"code": "pl",
"name": "Poland"
},
"name": "Wroclaw, Lower Silesian District, Poland"
},
"startDate": {
"month": 1,
"year": 2016
},
"title": "Job position"
},
{
"company": {
"id": 611639,
"industry": "Internet",
"name": "Company 2",
"size": "51-200",
"type": "Privately Held"
},
"endDate": {
"month": 2,
"year": 2016
},
"id": 1111,
"isCurrent": false,
"location": {
"country": {
"code": "pl",
"name": "Poland"
},
"name": "Wroclaw, Lower Silesian District, Poland"
},
"startDate": {
"month": 4,
"year": 2015
},
"summary": "Summary of job",
"title": "Senior Android Developer"
},
{
"company": {
"id": 1111,
"industry": "Internet",
"name": "Company 2",
"size": "51-200",
"type": "Privately Held"
},
"endDate": {
"month": 3,
"year": 2015
},
"id": 111111,
"isCurrent": false,
"location": {
"country": {
"code": "pl",
"name": "Poland"
},
"name": "Wroclaw, Lower Silesian District, Poland"
},
"startDate": {
"month": 4,
"year": 2014
},
"summary": "short summary",
"title": "Android/iOS Developer"
},
{
"company": {
"id": 11111,
"industry": "Internet",
"name": "Company 2",
"size": "51-200",
"type": "Privately Held"
},
"endDate": {
"month": 3,
"year": 2014
},
"id": 1111111,
"isCurrent": false,
"location": {
"country": {
"code": "pl",
"name": "Poland"
},
"name": "Wroclaw, Lower Silesian District, Poland"
},
"startDate": {
"month": 2,
"year": 2013
},
"summary": "android applications developer.\ncross-platform mobile application testing (iOS, Windows 8)",
"title": "Junior Android Developer"
},
{
"company": {
"id": 3333,
"industry": "Information Technology & Services",
"name": "Company 3",
"size": "201-500",
"type": "Privately Held"
},
"endDate": {
"month": 11,
"year": 2011
},
"id": 3333333,
"isCurrent": false,
"location": {
"country": {
"code": "pl",
"name": "Poland"
},
"name": "Warsaw, Masovian District, Poland"
},
"startDate": {
"month": 7,
"year": 2011
},
"summary": "Intern ",
"title": "Internship"
}
]
},
"publicProfileUrl": "https://www.linkedin.com/in/username/41219"
}
Which shows that the Android SDK's response is missing some job positions and a summary for a profile.
It turned out LinkedIn is not returning fields that have no value. If you don't setup a summary in your profile, then there won't be a field named summary in the response. (Don't expect anything like "summary" : null)
Additionally only first position is returned for a given profile while using r_basicprofile scope. It's confusing since this one profile is returned in the positions array.
based on https://developer.linkedin.com/docs/fields/basic-profile :
Positions - An object representing the member's current position.
See Position Fields for a description of the fields available within this object.
The reason why the API Console's response was having all job positions is because it asked for r_fullprofile scope while authenticating

Contentful Product Catalogue Error

I have download this CONTENTFUL CMS product catalogue demo ->https://github.com/contentful/product-catalogue-android. Im using android studio 1.4.
I have set the public token ID in the const.java accordingly but it still get error. At first i think maybe i have set a wrong setting at content_type field setting. But i still cannot find my solution. My error look like below and ive attached images for you to see,
10-23 18:56:18.820 2094-2147/catalogue.contentful E/SQLiteLog: (1) no such table: entry_nulundg0rwhzuvm0suntyxvdzufjrq
(1) no such table: entry_ne90ogm2rdjxuxfnb2fvs2dzbtgysw
This is my JSON statement from my CONTENTFUL "Product": content_type
{
"name": "Product",
"description": "",
"displayField": "productName",
"fields": [
{
"name": "productName",
"id": "productName",
"type": "Symbol",
"localized": false,
"validations": []
},
{
"name": "productDescription",
"id": "productDescription",
"type": "Symbol",
"localized": false,
"validations": []
},
{
"name": "sizetypecolor",
"id": "sizetypecolor",
"type": "Symbol"
},
{
"name": "images",
"id": "images",
"type": "Array",
"items": {
"type": "Link",
"linkType": "Asset",
"validations": []
},
"validations": []
},
{
"name": "categories",
"id": "categories",
"type": "Link",
"linkType": "Entry",
"validations": [
{
"linkContentType": [
"5In484EhYQS4ICSauCeAcE"
]
}
]
},
{
"name": "tags",
"id": "tags",
"type": "Array",
"items": {
"type": "Symbol",
"validations": []
},
"validations": []
},
{
"name": "price",
"id": "price",
"type": "Number",
"validations": []
},
{
"name": "brand",
"id": "brand",
"type": "Link",
"linkType": "Entry",
"validations": [
{
"linkContentType": [
"5gTQW9EcAwEYEc42WGgaEu"
]
}
]
},
{
"name": "quantity",
"id": "quantity",
"type": "Integer",
"localized": false,
"validations": []
},
{
"name": "sku",
"id": "sku",
"type": "Symbol",
"localized": false,
"validations": []
},
{
"name": "website",
"id": "website",
"type": "Symbol",
"localized": false,
"validations": []
}
],
"sys": {
"id": "4Ot8c6D2qQqgoaUKgYm82K",
"type": "ContentType",
"createdAt": "2015-10-23T13:04:54.705Z",
"createdBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "2P70I7YlhYLnC0iOpPYYwN"
}
},
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "b7di2z8ukacn"
}
},
"firstPublishedAt": "2015-10-23T13:04:59.904Z",
"publishedCounter": 17,
"publishedAt": "2015-10-23T17:33:41.088Z",
"publishedBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "2P70I7YlhYLnC0iOpPYYwN"
}
},
"publishedVersion": 41,
"version": 42,
"updatedAt": "2015-10-23T17:33:41.107Z",
"updatedBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "2P70I7YlhYLnC0iOpPYYwN"
}
}
}
}
And this is my Android Studio coding
public Product$$ModelHelper() {
fields.add(FieldMeta.builder().setId("productName").setName("name").setSqliteType("STRING").build());
fields.add(FieldMeta.builder().setId("productDescription").setName("description").setSqliteType("STRING").build());
fields.add(FieldMeta.builder().setId("sizetypecolor").setName("sizeTypeColor").setSqliteType("STRING").build());
fields.add(FieldMeta.builder().setId("image").setName("images").setArrayType("com.contentful.vault.Asset").build());
fields.add(FieldMeta.builder().setId("tags").setName("tags").setSqliteType("BLOB").setArrayType("java.lang.String").build());
fields.add(FieldMeta.builder().setId("categories").setName("categories").setArrayType("catalogue.contentful.vault.Category").build());
fields.add(FieldMeta.builder().setId("price").setName("price").setSqliteType("DOUBLE").build());
fields.add(FieldMeta.builder().setId("brand").setName("brand").setLinkType("ENTRY").build());
fields.add(FieldMeta.builder().setId("quantity").setName("quantity").setSqliteType("INT").build());
fields.add(FieldMeta.builder().setId("sku").setName("sku").setSqliteType("STRING").build());
fields.add(FieldMeta.builder().setId("website").setName("website").setSqliteType("STRING").build());
}
#Override
public List<FieldMeta> getFields() {
return fields;
}
#Override
public String getTableName() {
return "entry_ne90ogm2rdjxuxfnb2fvs2dzbtgysw";
}
My Android Studio project error screenshot
I really appreciate if anyone can help me with this problem.Because this app is a demo, i really think my problem is at my setting at CONTENTFUL, but maybe i need second opinion. Please help.
Thank you.
Hello Mario from Contentful here,
As I could see from the code you provided, it seems as if you changed the generated classes. Can you try changing the Product.java using the annotations provided by vault to create a Product matching your data?
This also needs to be done with all the other ContentTypes, you created in the Contentful-WebUI.

Categories

Resources