问题
Does Retrofit
remove the trailing slash from a relative url for an endpoint? I have tried adding the trailing slash on one of my endpoints but when i step through with a debugger and i look at the Call
object, if I drill into the delete.relativeUrl it does not show the trailing slash.
回答1:
You need to manually add the slash at the end of the base URL endpoint.
Let's say you have this two instances of retrofit:
No Slash
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
With Slash
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: add the trailing slash manually
回答2:
You can try the following code. Just remove slash with base url because back slash already added begin with end point url in GET method.
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";
来源:https://stackoverflow.com/questions/38758570/retrofit-trailing-slash-on-relative-urls