当前位置: 首页 > 知识库问答 >
问题:

REVERFIT应为BEGIN_ARRAY,但BEGIN_OBJECT位于onActivityResult的第1行第2列路径$

胡高朗
2023-03-14

因此,在我的应用程序中,用户可以为他们的饭菜拍照,通过使用TensorFlow的图像分类,它将在OnActivityResult中对饭菜进行分类。然后,使用Edamam食谱搜索API,它将向用户返回该餐的食谱。

@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)
    }
})

对于JSONPlaceHolderAPICallRecipes,我在活动的顶部创建了以下变量:

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:我已经包含了Recipe类。

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
        }
}

有人知道如何解决这个问题吗?

共有1个答案

孔光赫
2023-03-14

您的错误告诉您,它试图将一些不能成为列表的东西列成一个列表。在查看了json之后,我确实发现了一些其他人还没有发现的东西。

json的结构开始时不是一个列表,而是一个对象:

{
    "q": "Sandwich",
    "from": 0,
    "to": 1,
    "more": true,
    "count": 16247,
    "hits": [
       // recipe is in here
    ]
  }

这意味着,reverfit所期望的对象不是list 类型的对象,而是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
)
@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>

一些需要注意的事情:

  • 菜谱列表现在是具有菜谱
  • 命中列表
  • RecipeResponse中的字段不一定是必需的(这取决于您如何使用改装配置GSON)。命中字段是必需的
 类似资料: