android - Retrofit Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ in onActivityResult
问题描述
So in my app a user can take a picture of their meal and through the use of Tensorflow's image classification, it will classify what the meal is in onActivityResult
. Then, using Edamam Recipe Search API, it will return a recipe for that meal to the user.
When I run my app, everything is working fine and nothing is crashing, however when I use Retrofit Callback
, the onFailure
method is called and I get this error:
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
This is what I have in my RecipeApi
interface:
@GET("search")
fun searchRecipe(
@Query("q") query: String,
@Query("app_id") app_id: String,
@Query("app_key") app_key: String,
@Query("from") from: Int,
@Query("to") to: Int
): Call<List<Recipe>>
And this is what I have in my onActivityResult
in my MainActivity
:
retrofit = Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
jsonPlaceHolderApi = retrofit.create(RecipeApi::class.java)
call = jsonPlaceHolderApi.searchRecipe(text, Constants.API_ID, Constants.API_KEY, 0, 1)
call.enqueue(object : Callback<List<Recipe>> {
override fun onResponse(
call: Call<List<Recipe>>,
response: Response<List<Recipe>>
) {
if (!response.isSuccessful) {
println("Code: " + response.code())
return
}
recipes = response.body()!!
for (recipe: Recipe in recipes) {
var content: String = ""
content += "Yield: ${recipe.yield} + \n"
content += "Calories: ${recipe.calories} + \n"
content += "TotalWeight: ${recipe.totalWeight} + \n"
content += "Ingredients: ${recipe.ingredients} + \n"
content += "TotalNutrients: ${recipe.totalNutrients} + \n"
content += "TotalDaily: ${recipe.totalDaily} + \n\n"
println(content)
}
}
override fun onFailure(call: Call<List<Recipe>>, t: Throwable) {
println(t.message)
}
})
For jsonPlaceHolderApi
, call
, and recipes
, I created those variables at the top of my activity:
private lateinit var jsonPlaceHolderApi: RecipeApi
private lateinit var call: Call<List<Recipe>>
private lateinit var recipes: List<Recipe>
UPDATE 1:
I have included the JSON response after using OkHttp
.
{
"q": "Sandwich",
"from": 0,
"to": 1,
"more": true,
"count": 16247,
"hits": [
{
"recipe": {
"uri": "http://www.edamam.com/ontologies/edamam.owl#recipe_24b4a299a9afcd43720c49e0cd32a00b",
"label": "Cambridge Market Sandwich",
"image": "https://www.edamam.com/web-img/30b/30be47fc42c63feeeb23d465d8c7ea30.jpg",
"source": "Food52",
"url": "https://food52.com/recipes/12390-cambridge-market-sandwich",
"shareAs": "http://www.edamam.com/recipe/cambridge-market-sandwich-24b4a299a9afcd43720c49e0cd32a00b/sandwich",
"yield": 6,
"dietLabels": [],
"healthLabels": [
"Vegetarian",
"Peanut-Free",
"Tree-Nut-Free",
"Alcohol-Free",
"Immuno-Supportive"
],
"cautions": [
"Sulfites"
],
"ingredientLines": [
"1 fresh baguette, cut into sandwich parts",
"1 tablespoon good quality unsalted butter, per sandwich (so 4 tbsp if making 4 sandwiches)",
"1 apple, coxs or gala preferably (2 apples for 4 sandwiches)",
"1 slice of english blue cheese, preferably a stilton (again, or 4 slices for 4 sandwiches)"
],
"ingredients": [
{
"text": "1 fresh baguette, cut into sandwich parts",
"weight": 300,
"image": "https://www.edamam.com/food-img/470/47053c77e167539c64fef3f2a3249bb2.jpg"
},
{
"text": "1 tablespoon good quality unsalted butter, per sandwich (so 4 tbsp if making 4 sandwiches)",
"weight": 14.2,
"image": null
},
{
"text": "1 apple, coxs or gala preferably (2 apples for 4 sandwiches)",
"weight": 182,
"image": "https://www.edamam.com/food-img/42c/42c006401027d35add93113548eeaae6.jpg"
},
{
"text": "1 slice of english blue cheese, preferably a stilton (again, or 4 slices for 4 sandwiches)",
"weight": 28.35,
"image": "https://www.edamam.com/food-img/b44/b442cfc174a1a691dca574c9b7dcb47a.jpg"
}
],
"calories": 1112.5294999999999,
"totalWeight": 524.55,
"totalTime": 5,
"totalNutrients": {
"ENERC_KCAL": {
"label": "Energy",
"quantity": 1112.5294999999999,
"unit": "kcal"
},
"FAT": {
"label": "Fat",
"quantity": 27.23481,
"unit": "g"
},
"FASAT": {
"label": "Saturated",
"quantity": 14.224877500000002,
"unit": "g"
},
"FATRN": {
"label": "Trans",
"quantity": 0.48047599999999996,
"unit": "g"
},
"FAMS": {
"label": "Monounsaturated",
"quantity": 6.288785,
"unit": "g"
},
"FAPU": {
"label": "Polyunsaturated",
"quantity": 3.316726,
"unit": "g"
},
"CHOCDF": {
"label": "Carbs",
"quantity": 181.44611,
"unit": "g"
},
"FIBTG": {
"label": "Fiber",
"quantity": 10.968,
"unit": "g"
},
"SUGAR": {
"label": "Sugars",
"quantity": 32.92007,
"unit": "g"
},
"PROCNT": {
"label": "Protein",
"quantity": 38.910799999999995,
"unit": "g"
},
"CHOLE": {
"label": "Cholesterol",
"quantity": 51.792500000000004,
"unit": "mg"
},
"NA": {
"label": "Sodium",
"quantity": 2134.2729999999997,
"unit": "mg"
},
"CA": {
"label": "Calcium",
"quantity": 320.01599999999996,
"unit": "mg"
},
"MG": {
"label": "Magnesium",
"quantity": 111.9045,
"unit": "mg"
},
"K": {
"label": "Potassium",
"quantity": 621.724,
"unit": "mg"
},
"FE": {
"label": "Iron",
"quantity": 12.039125000000002,
"unit": "mg"
},
"ZN": {
"label": "Zinc",
"quantity": 3.95969,
"unit": "mg"
},
"P": {
"label": "Phosphorus",
"quantity": 448.14250000000004,
"unit": "mg"
},
"VITA_RAE": {
"label": "Vitamin A",
"quantity": 158.72099999999998,
"unit": "µg"
},
"VITC": {
"label": "Vitamin C",
"quantity": 8.372,
"unit": "mg"
},
"THIA": {
"label": "Thiamin (B1)",
"quantity": 2.1698715,
"unit": "mg"
},
"RIBF": {
"label": "Riboflavin (B2)",
"quantity": 1.441445,
"unit": "mg"
},
"NIA": {
"label": "Niacin (B3)",
"quantity": 14.910620000000002,
"unit": "mg"
},
"VITB6A": {
"label": "Vitamin B6",
"quantity": 0.44310700000000003,
"unit": "mg"
},
"FOLDFE": {
"label": "Folate equivalent (total)",
"quantity": 526.092,
"unit": "µg"
},
"FOLFD": {
"label": "Folate (food)",
"quantity": 184.09199999999998,
"unit": "µg"
},
"FOLAC": {
"label": "Folic acid",
"quantity": 201,
"unit": "µg"
},
"VITB12": {
"label": "Vitamin B12",
"quantity": 0.37001,
"unit": "µg"
},
"VITD": {
"label": "Vitamin D",
"quantity": 0.35475,
"unit": "µg"
},
"TOCPHA": {
"label": "Vitamin E",
"quantity": 1.357915,
"unit": "mg"
},
"VITK1": {
"label": "Vitamin K",
"quantity": 7.7783999999999995,
"unit": "µg"
},
"WATER": {
"label": "Water",
"quantity": 269.289915,
"unit": "g"
}
},
"totalDaily": {
"ENERC_KCAL": {
"label": "Energy",
"quantity": 55.62647499999999,
"unit": "%"
},
"FAT": {
"label": "Fat",
"quantity": 41.899707692307686,
"unit": "%"
},
"FASAT": {
"label": "Saturated",
"quantity": 71.12438750000001,
"unit": "%"
},
"CHOCDF": {
"label": "Carbs",
"quantity": 60.482036666666666,
"unit": "%"
},
"FIBTG": {
"label": "Fiber",
"quantity": 43.872,
"unit": "%"
},
"PROCNT": {
"label": "Protein",
"quantity": 77.82159999999999,
"unit": "%"
},
"CHOLE": {
"label": "Cholesterol",
"quantity": 17.264166666666668,
"unit": "%"
},
"NA": {
"label": "Sodium",
"quantity": 88.92804166666664,
"unit": "%"
},
"CA": {
"label": "Calcium",
"quantity": 32.001599999999996,
"unit": "%"
},
"MG": {
"label": "Magnesium",
"quantity": 26.643928571428575,
"unit": "%"
},
"K": {
"label": "Potassium",
"quantity": 13.228170212765958,
"unit": "%"
},
"FE": {
"label": "Iron",
"quantity": 66.88402777777779,
"unit": "%"
},
"ZN": {
"label": "Zinc",
"quantity": 35.997181818181815,
"unit": "%"
},
"P": {
"label": "Phosphorus",
"quantity": 64.02035714285715,
"unit": "%"
},
"VITA_RAE": {
"label": "Vitamin A",
"quantity": 17.635666666666662,
"unit": "%"
},
"VITC": {
"label": "Vitamin C",
"quantity": 9.302222222222223,
"unit": "%"
},
"THIA": {
"label": "Thiamin (B1)",
"quantity": 180.82262500000002,
"unit": "%"
},
"RIBF": {
"label": "Riboflavin (B2)",
"quantity": 110.88038461538463,
"unit": "%"
},
"NIA": {
"label": "Niacin (B3)",
"quantity": 93.19137500000001,
"unit": "%"
},
"VITB6A": {
"label": "Vitamin B6",
"quantity": 34.08515384615385,
"unit": "%"
},
"FOLDFE": {
"label": "Folate equivalent (total)",
"quantity": 131.523,
"unit": "%"
},
"VITB12": {
"label": "Vitamin B12",
"quantity": 15.417083333333332,
"unit": "%"
},
"VITD": {
"label": "Vitamin D",
"quantity": 2.365,
"unit": "%"
},
"TOCPHA": {
"label": "Vitamin E",
"quantity": 9.052766666666665,
"unit": "%"
},
"VITK1": {
"label": "Vitamin K",
"quantity": 6.481999999999999,
"unit": "%"
}
},
"digest": [
{
"label": "Fat",
"tag": "FAT",
"schemaOrgTag": "fatContent",
"total": 27.23481,
"hasRDI": true,
"daily": 41.899707692307686,
"unit": "g",
"sub": [
{
"label": "Saturated",
"tag": "FASAT",
"schemaOrgTag": "saturatedFatContent",
"total": 14.224877500000002,
"hasRDI": true,
"daily": 71.12438750000001,
"unit": "g"
},
{
"label": "Trans",
"tag": "FATRN",
"schemaOrgTag": "transFatContent",
"total": 0.48047599999999996,
"hasRDI": false,
"daily": 0,
"unit": "g"
},
{
"label": "Monounsaturated",
"tag": "FAMS",
"schemaOrgTag": null,
"total": 6.288785,
"hasRDI": false,
"daily": 0,
"unit": "g"
},
{
"label": "Polyunsaturated",
"tag": "FAPU",
"schemaOrgTag": null,
"total": 3.316726,
"hasRDI": false,
"daily": 0,
"unit": "g"
}
]
},
{
"label": "Carbs",
"tag": "CHOCDF",
"schemaOrgTag": "carbohydrateContent",
"total": 181.44611,
"hasRDI": true,
"daily": 60.482036666666666,
"unit": "g",
"sub": [
{
"label": "Carbs (net)",
"tag": "CHOCDF.net",
"schemaOrgTag": null,
"total": 170.47811000000002,
"hasRDI": false,
"daily": 0,
"unit": "g"
},
{
"label": "Fiber",
"tag": "FIBTG",
"schemaOrgTag": "fiberContent",
"total": 10.968,
"hasRDI": true,
"daily": 43.872,
"unit": "g"
},
{
"label": "Sugars",
"tag": "SUGAR",
"schemaOrgTag": "sugarContent",
"total": 32.92007,
"hasRDI": false,
"daily": 0,
"unit": "g"
},
{
"label": "Sugars, added",
"tag": "SUGAR.added",
"schemaOrgTag": null,
"total": 0,
"hasRDI": false,
"daily": 0,
"unit": "g"
}
]
},
{
"label": "Protein",
"tag": "PROCNT",
"schemaOrgTag": "proteinContent",
"total": 38.910799999999995,
"hasRDI": true,
"daily": 77.82159999999999,
"unit": "g"
},
{
"label": "Cholesterol",
"tag": "CHOLE",
"schemaOrgTag": "cholesterolContent",
"total": 51.792500000000004,
"hasRDI": true,
"daily": 17.264166666666668,
"unit": "mg"
},
{
"label": "Sodium",
"tag": "NA",
"schemaOrgTag": "sodiumContent",
"total": 2134.2729999999997,
"hasRDI": true,
"daily": 88.92804166666664,
"unit": "mg"
},
{
"label": "Calcium",
"tag": "CA",
"schemaOrgTag": null,
"total": 320.01599999999996,
"hasRDI": true,
"daily": 32.001599999999996,
"unit": "mg"
},
{
"label": "Magnesium",
"tag": "MG",
"schemaOrgTag": null,
"total": 111.9045,
"hasRDI": true,
"daily": 26.643928571428575,
"unit": "mg"
},
{
"label": "Potassium",
"tag": "K",
"schemaOrgTag": null,
"total": 621.724,
"hasRDI": true,
"daily": 13.228170212765958,
"unit": "mg"
},
{
"label": "Iron",
"tag": "FE",
"schemaOrgTag": null,
"total": 12.039125000000002,
"hasRDI": true,
"daily": 66.88402777777779,
"unit": "mg"
},
{
"label": "Zinc",
"tag": "ZN",
"schemaOrgTag": null,
"total": 3.95969,
"hasRDI": true,
"daily": 35.997181818181815,
"unit": "mg"
},
{
"label": "Phosphorus",
"tag": "P",
"schemaOrgTag": null,
"total": 448.14250000000004,
"hasRDI": true,
"daily": 64.02035714285715,
"unit": "mg"
},
{
"label": "Vitamin A",
"tag": "VITA_RAE",
"schemaOrgTag": null,
"total": 158.72099999999998,
"hasRDI": true,
"daily": 17.635666666666662,
"unit": "µg"
},
{
"label": "Vitamin C",
"tag": "VITC",
"schemaOrgTag": null,
"total": 8.372,
"hasRDI": true,
"daily": 9.302222222222223,
"unit": "mg"
},
{
"label": "Thiamin (B1)",
"tag": "THIA",
"schemaOrgTag": null,
"total": 2.1698715,
"hasRDI": true,
"daily": 180.82262500000002,
"unit": "mg"
},
{
"label": "Riboflavin (B2)",
"tag": "RIBF",
"schemaOrgTag": null,
"total": 1.441445,
"hasRDI": true,
"daily": 110.88038461538463,
"unit": "mg"
},
{
"label": "Niacin (B3)",
"tag": "NIA",
"schemaOrgTag": null,
"total": 14.910620000000002,
"hasRDI": true,
"daily": 93.19137500000001,
"unit": "mg"
},
{
"label": "Vitamin B6",
"tag": "VITB6A",
"schemaOrgTag": null,
"total": 0.44310700000000003,
"hasRDI": true,
"daily": 34.08515384615385,
"unit": "mg"
},
{
"label": "Folate equivalent (total)",
"tag": "FOLDFE",
"schemaOrgTag": null,
"total": 526.092,
"hasRDI": true,
"daily": 131.523,
"unit": "µg"
},
{
"label": "Folate (food)",
"tag": "FOLFD",
"schemaOrgTag": null,
"total": 184.09199999999998,
"hasRDI": false,
"daily": 0,
"unit": "µg"
},
{
"label": "Folic acid",
"tag": "FOLAC",
"schemaOrgTag": null,
"total": 201,
"hasRDI": false,
"daily": 0,
"unit": "µg"
},
{
"label": "Vitamin B12",
"tag": "VITB12",
"schemaOrgTag": null,
"total": 0.37001,
"hasRDI": true,
"daily": 15.417083333333332,
"unit": "µg"
},
{
"label": "Vitamin D",
"tag": "VITD",
"schemaOrgTag": null,
"total": 0.35475,
"hasRDI": true,
"daily": 2.365,
"unit": "µg"
},
{
"label": "Vitamin E",
"tag": "TOCPHA",
"schemaOrgTag": null,
"total": 1.357915,
"hasRDI": true,
"daily": 9.052766666666665,
"unit": "mg"
},
{
"label": "Vitamin K",
"tag": "VITK1",
"schemaOrgTag": null,
"total": 7.7783999999999995,
"hasRDI": true,
"daily": 6.481999999999999,
"unit": "µg"
},
{
"label": "Sugar alcohols",
"tag": "Sugar.alcohol",
"schemaOrgTag": null,
"total": 0,
"hasRDI": false,
"daily": 0,
"unit": "g"
},
{
"label": "Water",
"tag": "WATER",
"schemaOrgTag": null,
"total": 269.289915,
"hasRDI": false,
"daily": 0,
"unit": "g"
}
]
},
"bookmarked": false,
"bought": false
}
]
}
UPDATE 2: I have included the Recipe class.
class Recipe {
@SerializedName(value = "yield")
var yield: Int? = null
get() = field
set(value) {
field = value
}
@SerializedName(value = "calories")
var calories: Float? = null
get() = field
set(value) {
field = value
}
@SerializedName(value = "totalWeight")
var totalWeight: Float? = null
get() = field
set(value) {
field = value
}
@SerializedName(value = "ingredients")
var ingredients: List<Ingredient>? = null
get() = field
set(value) {
field = value
}
@SerializedName(value = "totalNutrients")
var totalNutrients: Map<String, NutrientInfo>? = null
get() = field
set(value) {
field = value
}
@SerializedName(value = "totalDaily")
var totalDaily: Map<String, NutrientInfo>? = null
get() = field
set(value) {
field = value
}
}
Does anyone know how to fix this issue?
解决方案
Your error tells you that it tries to make a list of something that can't be a list. After some looking at the json I did found something that others haven't found yet.
The structure of the json is not starting as a list but as an object:
{
"q": "Sandwich",
"from": 0,
"to": 1,
"more": true,
"count": 16247,
"hits": [
// recipe is in here
]
}
This means that the Object that retrofit should be expecting is not of a type List<Recipe>
but it should be an object of type RecipeResponse
(defined below)
data class RecipeResponse(
@SerializedName("count")
val count: Int = 0,
@SerializedName("from")
val from: Int = 0,
@SerializedName("hits")
val hits: List<Hit>,
@SerializedName("more")
val more: Boolean = false,
@SerializedName("q")
val q: String = "",
@SerializedName("to")
val to: Int = 0
)
data class Hit(
@SerializedName("bookmarked")
val bookmarked: Boolean = false,
@SerializedName("bought")
val bought: Boolean = false,
@SerializedName("recipe")
val recipe: Recipe
)
In Hit
you can use your recipe object
Don't forget to change the method in the interface to
@GET("search")
fun searchRecipe(
@Query("q") query: String,
@Query("app_id") app_id: String,
@Query("app_key") app_key: String,
@Query("from") from: Int,
@Query("to") to: Int
): Call<RecipeResponse>
Some things to note:
- The list of recipes is now a list of
Hit
s with aRecipe
- The fields that are in RecipeResponse are not necessarily needed (depending of how you have GSON configured with Retrofit). The hits field is neccesary
推荐阅读
- excel-formula - 有没有办法从连接结果中删除空换行符?
- spring - Websphere - Spring 集成 SSL 问题
- python - 没有烧瓶 sqlalchemy 或 ORM 的烧瓶登录
- markdown - 使用 pandoc-citeproc 输出参考书目而不引用任何内容
- angular - Angular 7 - 服务和订阅
- reactjs - ReactTable中列的自定义排序方法
- c - 打印句子中每个单词的首字母
- jquery - Bootbox - Confirm Modal Dialog Box 需要回调函数……但我有一个
- android - ZXing中的摄像头找到二维码后如何重启
- avro - Apache Flume 中的 Apache Avro 模式验证