【问题标题】:Duplicate Slashes in Retrofit URL改造 URL 中的重复斜杠
【发布时间】:2015-06-03 14:51:43
【问题描述】:

我遇到了重复斜杠的问题,我想知道 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 引起的。

我认为我的问题可以通过删除 "/{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

【问题讨论】:

  • 为什么不删除端点 URL 中的 /?像这样http://some.url.com
  • I tried manually stripping out the trailing slash from my base endpoint... 我也认为这会解决它,但它似乎来自注释中的前导斜杠以及我从服务器获取的路径。

标签: android url path retrofit


【解决方案1】:

此问题已在 Retrofit 2.0.0-beta3 中修复。它需要更改服务类中的方法签名/注释。原始 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 2016-01-10
    • 2015-01-21
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多