- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,在我的应用程序中,用户可以为他们的餐点拍照,并通过使用Tensorflow的图像分类,它将在onActivityResult
中对餐点进行分类。然后,使用Edamam Recipe Search API,它将把该餐的食谱返回给用户。
当我运行我的应用程序时,一切正常,并且没有崩溃,但是,当我使用Retrofit Callback
时,调用了onFailure
方法,但出现此错误:Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
这是我的RecipeApi
界面中的内容:
@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>>
这就是我的
onActivityResult
中的
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)
}
})
对于
jsonPlaceHolderApi
,
call
和
recipes
,我在 Activity 顶部创建了这些变量:
private lateinit var jsonPlaceHolderApi: RecipeApi
private lateinit var call: Call<List<Recipe>>
private lateinit var recipes: List<Recipe>
更新1:
OkHttp
之后,我已经包含了JSON响应。
{
"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
}
]
}
更新2:
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
}
}
有人知道如何解决此问题吗?
最佳答案
您的错误告诉您它试图将无法列出的内容列出来。
在查看了json之后,我确实发现了其他人尚未发现的东西。
json的结构不是以列表的形式而是以对象的形式开始的:
{
"q": "Sandwich",
"from": 0,
"to": 1,
"more": true,
"count": 16247,
"hits": [
// recipe is in here
]
}
这意味着 retrofit 应期望的对象不是
List<Recipe>
类型的,而应是
RecipeResponse
类型的对象(定义如下)
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
)
在
Hit
中,您可以使用配方对象
@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>
注意事项:
Hit
的Recipe
的列表关于android - 改造预期为BEGIN_ARRAY,但在onActivityResult的第1行第2列路径$处为BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63127277/
BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path)); BufferedImage image = Image
希望有人能够帮助我解决将我的 React 应用程序推送到 Heroku 时遇到的问题。 heroku 日志反复显示以下错误。 at=error code=H10 desc="App crashed"
我是 Kotlin 的新手,我正在经历这样的例子。 . . package com.example.lambda1 import spark.Spark.get fun main(args: Arra
如果您已经安装了 32 位 JDK,请在中定义一个 JAVA_HOME 变量 Computer>System Properties>System Setting>Enviorment VAriable
我正在开发一个独立于平台的应用程序。我收到一个文件 URL*。在 Windows 上,这些是: file:///Z:/folder%20to%20file/file.txt file://host/f
我在 OSX、Objective-C 上。 我有一个像 这样的路径/NSURL /Users/xxx/Desktop/image2.png 但我将它传递给第三方应用程序,该应用程序会像 excpect
我已经安装了 Android studio 和插件的 DART,FLUTTER 来启动 flutter,但是因为我在创建我的第一个 flutter 项目时无法提供 sdk 路径。 最佳答案 我试图找出
127.0.0.1:8000/api/仅包含来自第二个应用程序的 url,但我将两个 url 模块链接到相同的模式。甚至有可能做到这一点吗? 第一个应用程序: from django.urls imp
对于大量图像(大约 1k,加上相同数量的拇指,在大约 500 个文件夹中),我们要求网站上使用的所有图像 URI 都必须具有 SEO 优化路径。它们已经准备好并提供完整的路径结构(每个文件夹包含一个具
为什么 f 不是一个文件?什么可能导致这种情况? String currentPhotoPath = "file:/storage/sdcard0/Pictures/someFileName.
Gradle 中的项目名称或路径中允许使用哪些字符? 它是否与特定操作系统的目录名称中允许的字符相同(例如: http://en.wikipedia.org/wiki/Filename#Reserve
我有一个包含文件夹路径的表格。我需要找到层次结构中这些文件夹之间的所有“差距”。我的意思是,如果表格包含这 3 个文件夹: 'A' 'A\B\C' 'A\B\C\D\E\F\G' 我需要在层次结构中找
我在 Linux 服务器上的/home/subversion 中安装了 svn - 那里有一个 ROOT 文件夹,其中包含 db 和 conf 等文件夹。没有映射到项目名称的文件夹,请有人告诉我如何列
对于我的图像位置:/src/assets/bitmap/sample.jpg 给出了关键配置: context: resolve('src') output: { path: resolve('b
我需要创建带有圆角的 SVG 路径,以将它们导出到 DXF 进行切割。我的问题是角应该是圆弧,而不是贝塞尔曲线。 使用 arc 命令相对容易处理直角,因为半径也是从拐角到圆弧起点的距离。对于其他角度,
大家好,我正在玩 Airflow,我正在阅读这篇很有帮助的 tutorial .我正在寻求帮助以更好地了解 Admin->Connection 如何在 Conn Type: File (path) 方
我的目标是定义R将用于安装和搜索库的单个路径。我read可以通过更改Rprofile.site安装路径中的R文件来完成。我在那里尝试了两个命令: .libPaths("D:/RLibrary") .L
我有一个问题:当我在一个页面中时,我想返回到上一页。我使用 $routeProvider。如何读取之前的 url? 我尝试在我的 Controller 中使用此代码但不起作用... angular.m
我正在尝试将一个文件从我的主干合并到一个分支(wc),并且对于看起来位于当前合并操作中不涉及的分支上的路径出现奇怪的未找到路径错误。 例如,在我们的 svn 项目中,我们有: 分行 分支 0 分支 1
我有一个树数据序列化如下: 关系:P到C是“一对多”,C到P是“一对一”。所以列 P 可能有重复的值,但列 C 有唯一的值。 P, C 1, 2 1, 3 3, 4 2, 5 4, 6 # in da
我是一名优秀的程序员,十分优秀!