java—在okhttp中是否可以在查询参数后添加路径段?

uhry853o  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(336)

我有一个http url:

HttpUrl httpurl = new HttpUrl.Builder()
.scheme("https")
.host("www.example.com")
.addQueryParameter("parameter", "p")
.addPathSegment("extrasegment")
.build();

query参数总是最后一个结束。我怎样才能执行我想要的命令?
编辑:
我之所以尝试实现这一点,是因为我希望能够访问某些格式如下的端点: https://host/api/{parameter}/anothersegment

mspsb9vt

mspsb9vt1#

我推测(从最初的问题)需要以下内容:

https://www.example.com/?param=p/anothersegment

考虑到定义以下内容的uri规范:

scheme ":" hier-part [ "?" query ] [ "#" fragment ]

url如下所示:

scheme = https
hier-part = www.example.com/
query = param=p/anothersegment

你可以这样做:

HttpUrl httpurl = new HttpUrl.Builder()
.scheme("https")
.host("www.example.com")
.addEncodedQueryParameter("param", "p/anotersegment")
// Use `EncodedQueryParamter` to prevent escaping the slashes and other special characters. (You need to escape values yourself though)
.build();

从编辑中,可能有人猜测您希望实现以下目标:

https://www.example.com/foo=bar/baz=xy

在哪里 foo=bar 以及 baz=xy 只是可以添加更多的路径段 addPathSegment

相关问题