是否可以在 OkHttp 的查询参数后添加路径段?
Is it possible to add path segment after query parameter in OkHttp?
我有一个 http url:
HttpUrl httpurl = new HttpUrl.Builder()
.scheme("https")
.host("www.example.com")
.addQueryParameter("parameter", "p")
.addPathSegment("extrasegment")
.build();
查询参数总是最后一个。我怎样才能执行我想要的命令?
编辑:
我尝试实现此目的的原因是因为我希望能够访问某些格式如下的端点:
https://host/api/{parameter}/anothersegment
我认为(根据最初的问题)需要如下内容:
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
添加的更多路径段
我有一个 http url:
HttpUrl httpurl = new HttpUrl.Builder()
.scheme("https")
.host("www.example.com")
.addQueryParameter("parameter", "p")
.addPathSegment("extrasegment")
.build();
查询参数总是最后一个。我怎样才能执行我想要的命令?
编辑:
我尝试实现此目的的原因是因为我希望能够访问某些格式如下的端点:
https://host/api/{parameter}/anothersegment
我认为(根据最初的问题)需要如下内容:
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