改造道路更换:更换了整个路径(包括/) [英] Retrofit path replacements: replacement over whole path (including /)

查看:217
本文介绍了改造道路更换:更换了整个路径(包括/)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的设置,我得到的所有来自REST API我的资源的路径,从该API的初始调用。我们使用这种模式能够改变所有的资源路径不打破现有的所有应用程序版本的过程中。

我一直在玩弄改造,我试图创建将接受我传递给它作为一个字符串的任何路径的方法。我尝试看起来像这样

  @GET(/ {路径})
公共FooBar的getFooBar(@Path(路径)字符串路径);
 

我再试试如下调用它。

 字符串路径=富/酒吧;
api.getFooBar(路径);
 

不幸的是改造的URL恩codeS路径替换,我最终作出,而不是 / foo的请求 /富%2Fbar /条。有没有什么办法来禁用URL编码的路径更换或进行更换跨越多个路径段?不幸的是我甚至不知道有多少的路径段还有,它是所有的API控制。

解决方案

使用<一个href="http://square.github.io/retrofit/javadoc/retrofit/http/En$c$cdPath.html"><$c$c>@En$c$cdPath!而已。我将复制的Javadoc所以这个答案有更多的肉:

  

命名替换URL路径。值是使用将String.valueOf(对象)转换为字符串。值是按字面没有使用URL编码。请参阅 @Path 的URL编码相同。

使用这样的:

  @GET(/ {路径})
void示例(@恩codeDPATH(路径)字符串路径,..);
 

In my setup, I get all the paths for my resources from the REST API from an initial call to the API. We use this pattern to be able to change all the resource paths without breaking all existing app versions in the process.

I have been playing around with Retrofit and I tried to create a method that would accept any path I pass to it as a string. My try looks like this

@GET("/{path}")
public FooBar getFooBar(@Path("path") String path);

I then try to call it as follows.

String path = "foo/bar";
api.getFooBar(path);

Unfortunately Retrofit URL-Encodes the path replacement and I end up making a request to /foo%2Fbar instead of /foo/bar. Is there any way to disable URL-Encoding for path replacements or to make replacements spanning multiple path segments? Unfortunately I don't even know how many path segments there are, it is all controlled by the API.

解决方案

Use @EncodedPath! That's it. I'll copy the Javadoc so this answer has more meat:

Named replacement in the URL path. Values are converted to string using String.valueOf(Object). Values are used literally without URL encoding. See @Path for URL encoding equivalent.

Use it like this:

@GET("/{path}")
void example(@EncodedPath("path") String path, ..);

这篇关于改造道路更换:更换了整个路径(包括/)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆