gpt4 book ai didi

android - 改造路径替换 : replacement over whole path (including/)

转载 作者:IT老高 更新时间:2023-10-28 21:54:20 26 4
gpt4 key购买 nike

在我的设置中,我从 REST API 获取资源的所有路径,这些路径来自对 API 的初始调用。我们使用此模式能够更改所有资源路径,而不会在此过程中破坏所有现有应用程序版本。

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

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

然后我尝试如下调用它。

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

不幸的是,Retrofit URL-Encodes 路径替换,我最终向 /foo%2Fbar 而不是 /foo/bar 发出请求。有什么方法可以禁用路径替换的 URL 编码或进行跨越多个路径段的替换?可惜我什至不知道有多少个路径段,都是API控制的。

最佳答案

使用 @EncodedPath !而已。我将复制 Javadoc,这样这个答案就有更多内容了:

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.

像这样使用它:

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

关于android - 改造路径替换 : replacement over whole path (including/),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23833290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com