【问题标题】:Android Retrofit's @Path don't work. Why?Android Retrofit 的 @Path 不起作用。为什么?
【发布时间】:2021-06-21 14:04:59
【问题描述】:

我有一个应用程序,它从 api 获取琐事问题并显示。当我使用实际链接回答问题时,它可以工作 (https://trivia.willfry.co.uk/api/questions?categories=movies&limit=16) 。但是,当我为类别添加路径时,应用程序不起作用。

这是工作:

@GET("questions?categories=general_knowledge&limit=16")
Call<List<Questions>> general();

但是这是行不通的:

@GET("questions?categories={type}&limit=16")
Call<List<Questions>> getData(@Path("type") String type);

此外,当我从模拟器中删除应用程序并使用路径包含方法再次安装时,它只工作一次。不是第二次。

【问题讨论】:

    标签: android api path retrofit


    【解决方案1】:

    categories 不是路径参数,而是查询,改为这样做

    @GET("questions?limit=16")
    Call<List<Questions>> getData(@Query("categories") String categories);
    

    关于你提到的第二个问题,在提供的代码中有注意事项

    【讨论】:

    • 不客气,伙计。路径参数是那些。在路径中(它们总是出现在? 之前)并且查询参数是? 之后的参数,由&amp; 分隔
    猜你喜欢
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多