我有一个网址 http://example.com/{x}/push/{y}
我正在使用 OkHttp curl 它。
final HttpUrl httpUrl = HttpUrl
.parse("http://example.com/{x}/push/{y}")
.newBuilder()
???
.build();
是否可以设置这些
{x}
和 {y}
路径参数?我可以看到像
addPathSegment
这样的方法这在某种程度上是相关的,但不是我想要的。
最佳答案
这是一种可以帮助您入门的技术。
HttpUrl template = HttpUrl.parse("http://example.com/{a}/b/{c}");
HttpUrl.Builder builder = template.newBuilder();
for (int i = 0; i < template.pathSegments().size(); i++) {
String parameter = template.pathSegments().get(i);
String replacement = null;
if (parameter.equals("{a}")) {
replacement = "foo";
} else if (parameter.equals("{c}")) {
replacement = "bar";
}
if (replacement != null) {
builder.setPathSegment(i, replacement);
}
}
HttpUrl url = builder.build();
关于android - 是否可以使用 OkHttp 设置占位符路径参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36751755/