Retrofit trailing slash on relative urls

☆樱花仙子☆ 提交于 2020-07-19 06:13:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!