【问题标题】:Retrofit trailing slash on relative urls在相对 url 上改造斜杠
【发布时间】:2016-08-04 04:37:01
【问题描述】:
Retrofit 是否从端点的相对 url 中删除尾部斜杠?我尝试在我的一个端点上添加斜杠,但是当我使用调试器单步执行并查看 Call 对象时,如果我深入到 delete.relativeUrl 它不会显示斜杠。
【问题讨论】:
标签:
retrofit
retrofit2
endpoint
trailing-slash
【解决方案1】:
您需要在基本 URL 端点的末尾手动添加斜杠。
假设您有这两个改造实例:
没有斜线
Retrofit retrofitNoSlash = new Retrofit.Builder()
.baseUrl("https://example.com/api/v5")
.build();
-
@GET("something"): https://example.com/api/v5something
-
@GET("/something"): https://example.com/something
带斜线
Retrofit retrofitWithSlash = new Retrofit.Builder()
.baseUrl("https://example.com/api/v5/")
.build();
-
@GET("something"): https://example.com/api/v5/something
-
@GET("/something"): https://example.com/something
so:手动添加斜杠
【解决方案2】:
你可以试试下面的代码。只需删除带有基本 url 的斜杠,因为在 GET 方法中已经添加了以端点 url 开头的反斜杠。
NetworkInterface.java
@GET("/baking.json")
Call<List<ResponseModel>> getRecepies();
public static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URLs.RECEPIES_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
URLs.java
public static String RECEPIES_URL="https://d17h27t6h515a5.cloudfront.net/topher/2017/May/5907926b_baking";