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

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

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

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

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

域模型类

package com.example.intojetpackcompose.domain.model

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class Meal(
    val idMeal: String? = null,
    val strMeal : String? = null,
    val strDrinkAlternate : String? = null,
    val strCategory: String? = null,
    val strArea: String? = null,
    val strInstructions: String? = null,
    val strMealThumb : String? = null,
    val strTags: String? = null,
    val strYoutube: String? = null,
    val strIngredient1: String,
    val strIngredient2: String,
    val strIngredient3: String,
    val strIngredient4: String,
    val strIngredient5: String,
    val strIngredient6: String,
    val strIngredient7: String,
    val strIngredient8: String,
    val strIngredient9: String,
    val strIngredient10: String,
    val strIngredient11: String,
    val strIngredient12: String,
    val strIngredient13: String,
    val strIngredient14: String,
    val strIngredient15: String,
    val strIngredient16: String,
    val strIngredient17: String,
    val strIngredient18: String,
    val strIngredient19: String,
    val strIngredient20: String,
    val strMeasure1: String,
    val strMeasure2: String,
    val strMeasure3: String,
    val strMeasure4: String,
    val strMeasure5: String,
    val strMeasure6: String,
    val strMeasure7: String,
    val strMeasure8: String,
    val strMeasure9: String,
    val strMeasure10: String,
    val strMeasure11: String,
    val strMeasure12: String,
    val strMeasure13: String,
    val strMeasure14: String,
    val strMeasure15: String,
    val strMeasure16: String,
    val strMeasure17: String,
    val strMeasure18: String,
    val strMeasure19: String,
    val strMeasure20: String,
    val strSource: String? = null,
    val strImageSource: String? = null,
    val strCreativeCommonsConfirmed: String? = null,
    val dateModified: String? = null
): Parcelable

字符串

  • 配方服务接口 *
package com.example.intojetpackcompose.network.RetrofitService

import com.example.intojetpackcompose.BottomNavigationBar
import com.example.intojetpackcompose.Constants
import com.example.intojetpackcompose.Meal
import com.example.intojetpackcompose.network.Response.RecipeSearchResponse
import com.example.intojetpackcompose.network.mdoel.RecipeDTO
import retrofit2.http.GET
import retrofit2.http.Query

interface RecipeService {

    @GET("search.php?s=")
    suspend fun get (
        @Query("query") query: String
    ) : RecipeDTO

    @GET("lookup.php?i=")
    suspend fun getById(
        @Query("id") id: Int
    ) : RecipeDTO

}


RecipeDto类

package com.example.intojetpackcompose.network.mdoel

import com.google.gson.annotations.SerializedName

class RecipeDTO (

    @SerializedName("idMeal")
    var idMeal: String? = null,
    @SerializedName("strMeal")
    var strMeal : String? = null,
    @SerializedName("strDrinkAlternate")
    var strDrinkAlternate : String? = null,
    @SerializedName("strCategory")
    var strCategory: String? = null,
    @SerializedName("strArea")
    var strArea: String? = null,
    @SerializedName("strInstructions")
    var strInstructions: String? = null,
    @SerializedName("strMealThumb")
    var strMealThumb : String? = null,
    @SerializedName("strTags")
    var strTags: String? = null,
    @SerializedName("strYoutube")
    var strYoutube: String? = null,
    @SerializedName("strIngredient1")
    var strIngredient1: String,
    @SerializedName("strIngredient2")
    var strIngredient2: String,
    @SerializedName("strIngredient3")
    var strIngredient3: String,
    @SerializedName("strIngredient4")
    var strIngredient4: String,
    @SerializedName("strIngredient5")
    var strIngredient5: String,
    @SerializedName("strIngredient6")
    var strIngredient6: String,
    @SerializedName("strIngredient7")
    var strIngredient7: String,
    @SerializedName("strIngredient8")
    var strIngredient8: String,
    @SerializedName("strIngredient9")
    var strIngredient9: String,
    @SerializedName("strIngredient10")
    var strIngredient10: String,
    @SerializedName("strIngredient11")
    var strIngredient11: String,
    @SerializedName("strIngredient12")
    var strIngredient12: String,
    @SerializedName("strIngredient13")
    var strIngredient13: String,
    @SerializedName("strIngredient14")
    var strIngredient14: String,
    @SerializedName("strIngredient15")
    var strIngredient15: String,
    @SerializedName("strIngredient16")
    var strIngredient16: String,
    @SerializedName("strIngredient17")
    var strIngredient17: String,
    @SerializedName("strIngredient18")
    var strIngredient18: String,
    @SerializedName("strIngredient19")
    var strIngredient19: String,
    @SerializedName("strIngredient20")
    var strIngredient20: String,
    @SerializedName("strMeasure1")
    var strMeasure1: String,
    @SerializedName("strMeasure2")
    var strMeasure2: String,
    @SerializedName("strMeasure3")
    var strMeasure3: String,
    @SerializedName("strMeasure4")
    var strMeasure4: String,
    @SerializedName("strMeasure5")
    var strMeasure5: String,
    @SerializedName("strMeasure6")
    var strMeasure6: String,
    @SerializedName("strMeasure7")
    var strMeasure7: String,
    @SerializedName("strMeasure8")
    var strMeasure8: String,
    @SerializedName("strMeasure9")
    var strMeasure9: String,
    @SerializedName("strMeasure10")
    var strMeasure10: String,
    @SerializedName("strMeasure11")
    var strMeasure11: String,
    @SerializedName("strMeasure12")
    var strMeasure12: String,
    @SerializedName("strMeasure13")
    var strMeasure13: String,
    @SerializedName("strMeasure14")
    var strMeasure14: String,
    @SerializedName("strMeasure15")
    var strMeasure15: String,
    @SerializedName("strMeasure16")
    var strMeasure16: String,
    @SerializedName("strMeasure17")
    var strMeasure17: String,
    @SerializedName("strMeasure18")
    var strMeasure18: String,
    @SerializedName("strMeasure19")
    var strMeasure19: String,
    @SerializedName("strMeasure20")
    var strMeasure20: String,
    @SerializedName("strSource")
    var strSource: String? = null,
    @SerializedName("strImageSource")
    var strImageSource: String? = null,
    @SerializedName("strCreativeCommonsConfirmed")
    var strCreativeCommonsConfirmed: String? = null,
    @SerializedName("strModified")
    var dateModified: String? = null
)


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

class MainActivity : ComponentActivity() {
    @SuppressLint("CoroutineCreationDuringComposition")
    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Log.d("STEP1","STEP1")
            val service = Retrofit.Builder()
                .baseUrl("https://www.themealdb.com/api/json/v1/1/")
                .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
                .build()
                .create(RecipeService::class.java)

            CoroutineScope(IO).launch {
                try{
                    Log.d("STEP2", "STEP2")
                    val response = service.get("Arrabiata")
                    Log.d(
                        "RECIPE",
                        "${response.idMeal}"
                    )
                }
                catch (e: Exception){
                    Log.e("API_ERROR", e.message)
                    e.printStackTrace()
                }

            }

        }

    }

eivnm1vs

eivnm1vs1#

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

interface RecipeService {

    @GET("search.php?s=")
    suspend fun get (
        @Query("query") query: String
    ) : List<RecipeDTO>

    @GET("lookup.php?i=")
    suspend fun getById(
        @Query("id") id: Int
    ) : List<RecipeDTO>

}

字符串

dfddblmv

dfddblmv2#

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

interface RecipeService {

    @GET("search.php")
    suspend fun get (
        @Query("s") query: String
    ) : RecipeDTO

    @GET("lookup.php")
    suspend fun getById(
        @Query("i") id: Int
    ) : RecipeDTO

}

字符串

相关问题