Uri.fromParts和Uri.parse之间的区别? [英] DIfference between Uri.fromParts and Uri.parse?
问题描述
我正在为Android创建一个Intent,用于发送电子邮件.
I'm creating an Intent for Android, to send e-mails.
我对Uri.fromParts的行为感到困惑.
And I'm getting confused about the behavior of Uri.fromParts.
Mi代码: 效果很好!
Mi code: This works fine!
uri=Uri.parse(
"mailto:" + toAddress +
(subject != null ?
("?" + "subject=" + Uri.encode(subject)) :
"")
之前的工作正常,并以"mailto:john@doe.com?subject = Test
The previous work fine, and create an Uri in the form "mailto:john@doe.com?subject=Test
但是,如果我尝试使用Uri.from部件,请使用以下示例:
But if I try to use Uri.from parts, with this sample:
uriBuilder=Uri.fromParts("mailto",toAddress,null).buildUpon();
if (subject!=null) {
uriBuilder.appendQueryParameter("subject",subject);
}
uri=uriBuilder.build();
我得到一个错误.最后的uri是mailto:?subject = Test
I get an error. The final uri is mailto:?subject=Test
中间体是正确的,但是当我使用appendQueryParameter时,它将在mailto方案之后删除内容.
The intermediate is correct, but when I use appendQueryParameter, it removes the content after the mailto scheme.
你知道为什么吗?哪种方法可以做到这一点?
Do you know why? Which is the canonical way to do this?
推荐答案
根据给定的组件创建一个不透明的Uri.对ssp进行编码,这意味着该方法不能用于创建分层URI.
Creates an opaque Uri from the given components. Encodes the ssp which means this method cannot be used to create hierarchical URIs. 对此调用 When you call 我认为没有规范"的方式.只是不要混用分层和不透明的构建器. I don't think there's a "canonical" way. Just don't mix hierarchical and opaque builders. 有关幕后情况的详细信息,请阅读来源. For details on what happens under the hood, read the source. 这篇关于Uri.fromParts和Uri.parse之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
buildUpon()
时,Builder
包含方案,方案特定的部分(ssp)和片段(在您的情况下为null).buildUpon()
on this, the Builder
contains the scheme, scheme-specific part (ssp) and the fragment (null in your case).appendQueryParameter()
然后将Builder
转换为层次结构,删除不透明的ssp数据.appendQueryParameter()
then turns the Builder
to a hierarchical one, deleting the opaque ssp data.