我有一个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 ]
网址看起来像这样:
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
添加这些路径段