【问题标题】:Passing ID in GET request URL through Model class in Retrofit通过 Retrofit 中的 Model 类在 GET 请求 URL 中传递 ID
【发布时间】:2019-04-05 09:43:55
【问题描述】:

这是删除记录的API URL

GET : localhost:4000/course/delete/5bd9a270c6a31620e0b7b3d8

5bd9a270c6a31620e0b7b3d8 是服务器和模型类中的 ID。我想调用一个 API 并将其动态传递给 url 以删除记录。

如何编写接口调用请求

@GET("course/delete/?id?") //how to write
Call<List<Course>> deleteRecords();

我们将不胜感激

【问题讨论】:

标签: java android json api retrofit2


【解决方案1】:

去做吧:

@GET("course/delete/{id}")
Call<List<Course>> deleteRecords(@Path("id") String id);

然后调用:

retrofit.deleteRecords("5bd9a270c6a31620e0b7b3d8");

【讨论】:

  • 感谢您的回复。但我收到此错误 java.lang.IllegalArgumentException:@Path 参数名称必须匹配 \{([a-zA-Z][a-zA-Z0-9_-]*)\}。找到:_id(参数#1)我是通过这个来调用的。 Call> call = serviceRetro.deleteRecords(id);
  • 在调试中我已经检查过了。 id 正确获取值
  • 只需将@Path("_id") 替换为@Path("id") 用于deleteRecords 方法中的参数即可。
  • 完美运行。谢谢!
【解决方案2】:

试试这个definition

@GET("course/delete/{id}")
Call<List<Course>> deleteRecords(@Path("id") String id));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 2018-06-24
    相关资源
    最近更新 更多