【问题标题】:Retrofit API must not have replace block改造 API 不能有替换块
【发布时间】:2021-08-02 15:09:33
【问题描述】:

我想从 API 获取数据但收到此错误 以下是课程 ID 参数为 5012 的 URL 示例:https://dth.cohota.com/api/v1/calendar_events/?all_events=true&type=assignment&context_codes[]=course_5012

java.lang.IllegalArgumentException:URL 查询字符串“all_events=true&type=assignment&context_codes[]=course_{courseId}”不能有替换块。对于动态查询参数,请使用@Query。

这是我的代码

 @GET("calendar_events/?all_events=true&type=assignment&context_codes[]=course_{courseId}")
    fun getCourseWithSyllabus(@Query("courseId") courseId: Long): Call<Course>

根据官方文档,我必须使用@Query,我正在使用它,但我也得到了错误。 感谢您的回复。

【问题讨论】:

    标签: java android api kotlin retrofit


    【解决方案1】:

    该错误意味着您只能使用硬编码的查询参数,即? 之后的内容。对于动态查询参数,使用类似

    @Query("context_codes[]") contextCodes
    

    并提供像"course_$courseId" 这样的值作为其值。

    您可以检查 Retrofit source 以详细了解异常的来源。

    【讨论】:

    • 感谢您的回复。与原始代码一样,它尝试将代码设置为 '@Path("courseId") 但同样的错误。
    • 所以我需要把params(context_codes[])的类型改成List,对吧?
    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 2014-08-27
    • 2017-12-12
    • 2018-02-04
    • 1970-01-01
    • 2016-06-28
    • 2020-01-22
    • 1970-01-01
    相关资源
    最近更新 更多