如何在中心创建带有主题标签的Android Uri



我有以下 Uri 想要构造。

https://www.example.com/sub/#/action?firstparam=123456&secondparam=abcdef

我使用 Android Uri.Builder 来构建 Uri

Uri uri = new Uri.Builder().scheme(HTTPS_SCHEME).authority("www.example.com").appendPath("sub").appendPath("#").appendPath("action")appendQueryParameter(
            "firstparam", first).appendQueryParameter("secondparam", second).build();

但是主题标签是编码的,将导致以下 Uri

https://www.example.com/sub/%23/action?firstparam=123456&secondparam=abcdef

如何预防这种情况?我尝试使用fragment但它在 Uri 的末尾添加了主题标签。

这就是

Uri.Builder的工作方式。它对非安全 URL 字符进行编码,这些字符对其十六进制值具有特殊含义。在您的情况下,#被编码为 %23

要防止此使用,请执行以下操作:

builder.appendEncodedPath("#")

最新更新