首页 > 解决方案 > Retrofit 中这个 URL 的接口是什么

问题描述

Get方法中:

http://xyz.erprnd.com/api/v1/customers/get?yearmonth=201807

我的设计界面:

@Headers({ "Content-Type: application/json;charset=UTF-8"})
@GET("customers/{get}/{yearmonthValue}")
Call<CustResponse> CustomerData(@HeaderMap Map<String, String> headers,
                                @Path("get") String get,
                                @Query("yearmonth") String yearmonth,
                                @Path("yearmonthValue") String yearmonthValue,
                                @Header("Authorization") String authHeader);

我可以改变什么来使它正常工作?

标签: androidretrofit2

解决方案


首先,在构建 Retrofit 时,仔细检查您是否将“ http://xyz.erprnd.com/api/v1 ”作为您的baseUrl传递。

然后,根据您发布的 URL,界面应如下所示:

@Headers({ "Content-Type: application/json;charset=UTF-8"})
@GET("customers/get")
Call<CustResponse> CustomerData(@HeaderMap Map<String, String> headers,
                                @Header("Authorization") String authHeader,
                                @Query("yearmonth") String yearmonth);

当你定义查询参数时,你只需要添加注解@Query("param_name") 然后传入的变量的值就是transform intro: param_name=variable_value

另外,我删除了您的 @Path("get") 参数,因为 URL 的那部分似乎永远/get不会改变。不幸的是,我无法测试它,因为我没有对您的 API 的授权。


推荐阅读