改造中的重复斜杠 URL

Duplicate Slashes in Retrofit URL

我 运行 遇到了重复斜杠的问题,我想知道 Retrofit 是否内置了解决方案。

我的服务器提供了我们应该使用的基础URL,它看起来像这样:.setEndpoint(http://some.url.com/)

服务器还向下传递一个路径 URI,该 URI 应附加到该端点(带有各种请求)。所以我的服务器可能会发回 /channels/videos.

通过以下方法将其交给 Retrofit

@GET("/{uri}")
void GET(@Header("Authorization") String authHeader, @Path(value = "uri", encode = false) String uri,
         @Header("Cache-Control") String cacheHeaderValue, Callback<Object> callback);

这是有问题的,因为被 GET 方法击中的 URL 是 http://some.url.com//channels/videos,在我的情况下它不能正常工作。

我尝试从我的基本端点手动删除尾部斜杠 - 但我仍然看到一个重复的斜杠,我假设它是由 "/{uri}"/channels/videos.[=24= 引起的]

我认为我的问题可以通过删除 "/{uri}" 中的前导斜杠来解决,但 Retrofit 不允许这样做。并且删除我从服务器返回的路径 URI 中的前导斜杠并不完全可行。

throw methodError("URL path \"%s\" must start with '/'.", path);

retrofit.RetrofitError: GET: URL path "{uri}" must start with '/'.

这个问题还有其他解决办法吗?

相关链接:

Possible duplicate but they describe it a little differently

Jake Wharton saying it should be de-duped in what I think is the situation I'm describing

Maybe what Jake Wharton was referencing

Currently unanswered issue asking Jake this same question

此问题已在 Retrofit 2.0.0-beta3 中修复。它需要更改服务 class 中的方法 signature/annotations。原始 GET 方法的签名现在是:

@GET
Call<Object> GET(@Header("Authorization") String authHeader, @Url String uri,
                 @QueryMap Map<String, String> options, @Header("Cache-Control") String cacheHeaderValue);

这是使用compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'

如果您还在使用以前版本的 Retrofit,我使用以下帮助程序(在传递给 GET 的 uri 上调用它)暂时解决了这个问题:

public static String validateUri(String uri) {
  if (uri.charAt(0) == '/') {
      uri = uri.substring(1);
  }
  return uri;
}