You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用TMDB API时getMovieByGenre报with_genres参数未传入错误排查

解决TMDB API调用中"No value passed for parameter 'with_genres'"错误

问题场景

开发Android电影APP时,用户点击分类(genre)后加载对应分类电影到HomeFragment,调用TMDB API实现该功能时触发错误提示:

No value passed for parameter 'with_genres'

问题根源

查看关键代码后发现参数传参错位:

  • ApiInterface中getMoviesByGenres方法的参数顺序为:page → 带默认值的api_key → with_genres
  • ApiRepository调用该接口时仅传入page和with_genres,导致传入的with_genres被错误赋值给api_key参数,真正的with_genres参数未被传入,触发报错。

关键代码片段:

// ApiInterface中的方法
@GET("discover/movie")
suspend fun getMoviesByGenres(
    @Query("page") page: Int,
    @Query("api_key") api_key: String = API_KEY,
    @Query("with_genres") with_genres: String
): Response<MovieResponse>

// ApiRepository中的调用
suspend fun getMovieByGenre(page: Int,with_genres: String) = 
    apiInterface.getMoviesByGenres(page,with_genres)

修复方案

有两种可靠的修复方式:

方式1:调用时使用命名参数

在ApiRepository调用接口时显式指定参数名,避免顺序匹配错误:

suspend fun getMovieByGenre(page: Int,with_genres: String) = 
    apiInterface.getMoviesByGenres(page = page, with_genres = with_genres)

方式2:调整接口参数顺序(推荐)

将带默认值的api_key参数移到方法参数列表的末尾,符合Kotlin函数参数的最佳实践(默认参数通常放在最后),这样调用时按顺序传参即可正确匹配:

// 修改后的ApiInterface方法
@GET("discover/movie")
suspend fun getMoviesByGenres(
    @Query("page") page: Int,
    @Query("with_genres") with_genres: String,
    @Query("api_key") api_key: String = API_KEY
): Response<MovieResponse>

// ApiRepository调用无需修改,按顺序传参即可
suspend fun getMovieByGenre(page: Int,with_genres: String) = 
    apiInterface.getMoviesByGenres(page,with_genres)

内容的提问来源于stack exchange,提问作者Umesh Maurya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 09:26:23