是否可以使用 Uri.Builder 而没有“//”部分?

发布于 2024-12-21 19:28:58 字数 791 浏览 1 评论 0 原文

我正在尝试构建一个 mailto: uri 来使用 GMail 应用程序发送邮件。 我想使用 android.net.Uri.Builder 类来执行此操作,但生成的 uri 格式为 mailto://[电子邮件受保护],这使 GMail 应用认为收件人是 //[电子邮件受保护],而不仅仅是 [电子邮件受保护]

我最终这样做了:

String uriStr = uriBuilder.toString();
uriStr = uriStr.replaceAll("//", "");
final Uri uri = Uri.parse(uriStr);

但显然,这是一个丑陋的黑客......

是否没有办法在没有 // 部分的情况下构建 uri?

I am trying to build a mailto: uri to send a mail using the GMail app.
I would like to use the android.net.Uri.Builder class to do this, but the resulting uri is in the form mailto://[email protected], which makes the GMail app think the recipient is //[email protected], instead of just [email protected].

I ended up doing this:

String uriStr = uriBuilder.toString();
uriStr = uriStr.replaceAll("//", "");
final Uri uri = Uri.parse(uriStr);

but clearly, this is an ugly hack...

Is there no way to build the uri without the // part?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情场扛把子 2024-12-28 19:28:59

这里有几个问题。虽然可以删除 // 部分,但您将丢失查询字符串。主要问题是 Uri.Builder 不允许您使用不透明 URI 的查询(不透明 URI 是绝对 URI,其特定于方案的部分不以斜杠字符开头,例如 mailto: URI )。

也就是说,您应该使用 uriBuilder.opaquePart() 而不是 uriBuilder.authority() ,因为后者 隐式将您的 URI 设置为分层,即非不透明。这将消除 //,但是您缺少查询部分,并且无法设置它,因为对 uriBuilder.appendQueryParameter() 的任何调用也会 <一个href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/net/Uri.java#Uri.Builder.appendQueryParameter%28java .lang.String%2Cjava.lang.String%29" rel="nofollow noreferrer">隐含分层 URI。

长话短说,要构造一个包含查询的不透明 mailto: URI,您必须使用

Uri uri = Uri.parse("mailto:[email protected]?subject=title&body=text");

它。当然,文字 titletext 应该被 Uri.encode() 编辑。

There are several issues here. While it is possible to get rid of the // part, you will loose the query strings then. The main problem is that Uri.Builder won't let you use queries with opaque URIs (an opaque URI is an absolute URI whose scheme-specific part does not begin with a slash character, like mailto: URIs).

That said, you should use uriBuilder.opaquePart() instead of uriBuilder.authority() because the latter implicitly sets your URI to hierarchical, i.e. non-opaque. This will get rid of the //, but you're lacking the query part then, and you cannot set it, because any call to uriBuilder.appendQueryParameter() also implies a hierarchical URI.

Long story short, to construct an opaque mailto: URI that includes queries, you'll have to use

Uri uri = Uri.parse("mailto:[email protected]?subject=title&body=text");

instead. Of course, the literal title and text should be Uri.encode()ed.

芸娘子的小脾气 2024-12-28 19:28:59

sschuberth 给出的答案很好地解释了正在发生的事情,但作为一个更实际的答案(毕竟你确实想要正确转义参数等),我使用了两个构建器来解决这个问题:

Builder builder1 = new Builder();
builder1.scheme("mailto");
builder1.opaquePart(emailAddress);

Builder builder2 = new Builder();
builder2.appendQueryParameter("subject", subject);
builder2.appendQueryParameter("body", body);

Uri uri = Uri.parse(builder1.toString() + builder2.toString());

您可能不想在具有数百万个地址的紧密循环中执行此操作,但对于一般用途,我认为这应该没问题。

The answer given by sschuberth is a good explanation of what's going on, but as a more practical answer (you do want to properly escape parameters, etc. after all), I used two builders to get around this:

Builder builder1 = new Builder();
builder1.scheme("mailto");
builder1.opaquePart(emailAddress);

Builder builder2 = new Builder();
builder2.appendQueryParameter("subject", subject);
builder2.appendQueryParameter("body", body);

Uri uri = Uri.parse(builder1.toString() + builder2.toString());

You probably don't want to do this in a tight loop with millions of addresses, but for general use I think this should be fine.

递刀给你 2024-12-28 19:28:59

sschuberth的回答简洁得多kabuko 的,所以这里有一个也涵盖编码的变体:

Uri uri = Uri.parse(
    String.format("mailto:%s?subject=%s",
        Uri.encode(recipient),
        Uri.encode(subject)
    )
);

sschuberth's answer is much briefer than kabuko's, so here's a variant that also covers encoding:

Uri uri = Uri.parse(
    String.format("mailto:%s?subject=%s",
        Uri.encode(recipient),
        Uri.encode(subject)
    )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文