kotlin 调用API服务时获取null Android App

mfuanj7w  于 2023-08-06  发布在  Kotlin
关注(0)|答案(2)|浏览(151)

我正在使用TheMealDB的免费API服务来构建一个食谱应用程序,但当我试图访问API以获取数据时,我总是得到一个 null 示例。

TheMealDB的网站是:https://www.themealdb.com/api.php

我使用的URL是:www.themealdb.com/api/json/v1/1/search.php?s={Query}
我的课程是:

域模型类

  1. package com.example.intojetpackcompose.domain.model
  2. import android.os.Parcelable
  3. import kotlinx.parcelize.Parcelize
  4. @Parcelize
  5. data class Meal(
  6. val idMeal: String? = null,
  7. val strMeal : String? = null,
  8. val strDrinkAlternate : String? = null,
  9. val strCategory: String? = null,
  10. val strArea: String? = null,
  11. val strInstructions: String? = null,
  12. val strMealThumb : String? = null,
  13. val strTags: String? = null,
  14. val strYoutube: String? = null,
  15. val strIngredient1: String,
  16. val strIngredient2: String,
  17. val strIngredient3: String,
  18. val strIngredient4: String,
  19. val strIngredient5: String,
  20. val strIngredient6: String,
  21. val strIngredient7: String,
  22. val strIngredient8: String,
  23. val strIngredient9: String,
  24. val strIngredient10: String,
  25. val strIngredient11: String,
  26. val strIngredient12: String,
  27. val strIngredient13: String,
  28. val strIngredient14: String,
  29. val strIngredient15: String,
  30. val strIngredient16: String,
  31. val strIngredient17: String,
  32. val strIngredient18: String,
  33. val strIngredient19: String,
  34. val strIngredient20: String,
  35. val strMeasure1: String,
  36. val strMeasure2: String,
  37. val strMeasure3: String,
  38. val strMeasure4: String,
  39. val strMeasure5: String,
  40. val strMeasure6: String,
  41. val strMeasure7: String,
  42. val strMeasure8: String,
  43. val strMeasure9: String,
  44. val strMeasure10: String,
  45. val strMeasure11: String,
  46. val strMeasure12: String,
  47. val strMeasure13: String,
  48. val strMeasure14: String,
  49. val strMeasure15: String,
  50. val strMeasure16: String,
  51. val strMeasure17: String,
  52. val strMeasure18: String,
  53. val strMeasure19: String,
  54. val strMeasure20: String,
  55. val strSource: String? = null,
  56. val strImageSource: String? = null,
  57. val strCreativeCommonsConfirmed: String? = null,
  58. val dateModified: String? = null
  59. ): Parcelable

字符串

  • 配方服务接口 *
  1. package com.example.intojetpackcompose.network.RetrofitService
  2. import com.example.intojetpackcompose.BottomNavigationBar
  3. import com.example.intojetpackcompose.Constants
  4. import com.example.intojetpackcompose.Meal
  5. import com.example.intojetpackcompose.network.Response.RecipeSearchResponse
  6. import com.example.intojetpackcompose.network.mdoel.RecipeDTO
  7. import retrofit2.http.GET
  8. import retrofit2.http.Query
  9. interface RecipeService {
  10. @GET("search.php?s=")
  11. suspend fun get (
  12. @Query("query") query: String
  13. ) : RecipeDTO
  14. @GET("lookup.php?i=")
  15. suspend fun getById(
  16. @Query("id") id: Int
  17. ) : RecipeDTO
  18. }


RecipeDto类

  1. package com.example.intojetpackcompose.network.mdoel
  2. import com.google.gson.annotations.SerializedName
  3. class RecipeDTO (
  4. @SerializedName("idMeal")
  5. var idMeal: String? = null,
  6. @SerializedName("strMeal")
  7. var strMeal : String? = null,
  8. @SerializedName("strDrinkAlternate")
  9. var strDrinkAlternate : String? = null,
  10. @SerializedName("strCategory")
  11. var strCategory: String? = null,
  12. @SerializedName("strArea")
  13. var strArea: String? = null,
  14. @SerializedName("strInstructions")
  15. var strInstructions: String? = null,
  16. @SerializedName("strMealThumb")
  17. var strMealThumb : String? = null,
  18. @SerializedName("strTags")
  19. var strTags: String? = null,
  20. @SerializedName("strYoutube")
  21. var strYoutube: String? = null,
  22. @SerializedName("strIngredient1")
  23. var strIngredient1: String,
  24. @SerializedName("strIngredient2")
  25. var strIngredient2: String,
  26. @SerializedName("strIngredient3")
  27. var strIngredient3: String,
  28. @SerializedName("strIngredient4")
  29. var strIngredient4: String,
  30. @SerializedName("strIngredient5")
  31. var strIngredient5: String,
  32. @SerializedName("strIngredient6")
  33. var strIngredient6: String,
  34. @SerializedName("strIngredient7")
  35. var strIngredient7: String,
  36. @SerializedName("strIngredient8")
  37. var strIngredient8: String,
  38. @SerializedName("strIngredient9")
  39. var strIngredient9: String,
  40. @SerializedName("strIngredient10")
  41. var strIngredient10: String,
  42. @SerializedName("strIngredient11")
  43. var strIngredient11: String,
  44. @SerializedName("strIngredient12")
  45. var strIngredient12: String,
  46. @SerializedName("strIngredient13")
  47. var strIngredient13: String,
  48. @SerializedName("strIngredient14")
  49. var strIngredient14: String,
  50. @SerializedName("strIngredient15")
  51. var strIngredient15: String,
  52. @SerializedName("strIngredient16")
  53. var strIngredient16: String,
  54. @SerializedName("strIngredient17")
  55. var strIngredient17: String,
  56. @SerializedName("strIngredient18")
  57. var strIngredient18: String,
  58. @SerializedName("strIngredient19")
  59. var strIngredient19: String,
  60. @SerializedName("strIngredient20")
  61. var strIngredient20: String,
  62. @SerializedName("strMeasure1")
  63. var strMeasure1: String,
  64. @SerializedName("strMeasure2")
  65. var strMeasure2: String,
  66. @SerializedName("strMeasure3")
  67. var strMeasure3: String,
  68. @SerializedName("strMeasure4")
  69. var strMeasure4: String,
  70. @SerializedName("strMeasure5")
  71. var strMeasure5: String,
  72. @SerializedName("strMeasure6")
  73. var strMeasure6: String,
  74. @SerializedName("strMeasure7")
  75. var strMeasure7: String,
  76. @SerializedName("strMeasure8")
  77. var strMeasure8: String,
  78. @SerializedName("strMeasure9")
  79. var strMeasure9: String,
  80. @SerializedName("strMeasure10")
  81. var strMeasure10: String,
  82. @SerializedName("strMeasure11")
  83. var strMeasure11: String,
  84. @SerializedName("strMeasure12")
  85. var strMeasure12: String,
  86. @SerializedName("strMeasure13")
  87. var strMeasure13: String,
  88. @SerializedName("strMeasure14")
  89. var strMeasure14: String,
  90. @SerializedName("strMeasure15")
  91. var strMeasure15: String,
  92. @SerializedName("strMeasure16")
  93. var strMeasure16: String,
  94. @SerializedName("strMeasure17")
  95. var strMeasure17: String,
  96. @SerializedName("strMeasure18")
  97. var strMeasure18: String,
  98. @SerializedName("strMeasure19")
  99. var strMeasure19: String,
  100. @SerializedName("strMeasure20")
  101. var strMeasure20: String,
  102. @SerializedName("strSource")
  103. var strSource: String? = null,
  104. @SerializedName("strImageSource")
  105. var strImageSource: String? = null,
  106. @SerializedName("strCreativeCommonsConfirmed")
  107. var strCreativeCommonsConfirmed: String? = null,
  108. @SerializedName("strModified")
  109. var dateModified: String? = null
  110. )


主要活动(这里我只是测试我是否能捕获数据)

  1. class MainActivity : ComponentActivity() {
  2. @SuppressLint("CoroutineCreationDuringComposition")
  3. @OptIn(ExperimentalMaterial3Api::class)
  4. override fun onCreate(savedInstanceState: Bundle?) {
  5. super.onCreate(savedInstanceState)
  6. setContent {
  7. Log.d("STEP1","STEP1")
  8. val service = Retrofit.Builder()
  9. .baseUrl("https://www.themealdb.com/api/json/v1/1/")
  10. .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
  11. .build()
  12. .create(RecipeService::class.java)
  13. CoroutineScope(IO).launch {
  14. try{
  15. Log.d("STEP2", "STEP2")
  16. val response = service.get("Arrabiata")
  17. Log.d(
  18. "RECIPE",
  19. "${response.idMeal}"
  20. )
  21. }
  22. catch (e: Exception){
  23. Log.e("API_ERROR", e.message)
  24. e.printStackTrace()
  25. }
  26. }
  27. }
  28. }

eivnm1vs

eivnm1vs1#

API返回一个项目列表,但函数只需要一个项目。事情应该是这样的:

  1. interface RecipeService {
  2. @GET("search.php?s=")
  3. suspend fun get (
  4. @Query("query") query: String
  5. ) : List<RecipeDTO>
  6. @GET("lookup.php?i=")
  7. suspend fun getById(
  8. @Query("id") id: Int
  9. ) : List<RecipeDTO>
  10. }

字符串

dfddblmv

dfddblmv2#

您遇到问题,因为您API服务类不是传递有效参数你可以这样写来得到回应:
你不需要写信吗?=当您使用查询参数时,您可以像这样传递参数,您将得到成功响应。

  1. interface RecipeService {
  2. @GET("search.php")
  3. suspend fun get (
  4. @Query("s") query: String
  5. ) : RecipeDTO
  6. @GET("lookup.php")
  7. suspend fun getById(
  8. @Query("i") id: Int
  9. ) : RecipeDTO
  10. }

字符串

展开查看全部

相关问题