是否可以使用Uri.Builder而不使用"//"部分? [英] Is it possible to use Uri.Builder and not have the "//" part?

查看:169
本文介绍了是否可以使用Uri.Builder而不使用"//"部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建 mailto: uri以使用GMail应用发送邮件.我想使用 android.net.Uri.Builder 类执行此操作,但是生成的uri的格式为 mailto://test@example.com ,这使GMail应用程序认为收件人是//test@example.com ,而不只是 test@example.com .

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://test@example.com, which makes the GMail app think the recipient is //test@example.com, instead of just test@example.com.

我最终这样做:

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

但是很明显,这是一个丑陋的骇客...

but clearly, this is an ugly hack...

如果没有//部分,是否无法构建uri?

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

推荐答案

这里有几个问题.尽管可以摆脱//部分,但是您将随后松开查询字符串.主要问题是Uri.Builder不允许您使用不透明URI进行查询(不透明URI是绝对URI,其特定于方案的部分不以斜杠字符开头,例如 mailto: URIs).

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).

也就是说,您应该使用 uriBuilder.opaquePart()而不是 uriBuilder.authority(),因为后者将您的URI隐式设置为分层,即非透明.这将摆脱//,但是您缺少查询部分,因此无法进行设置,因为对 uriBuilder.appendQueryParameter()的任何调用也

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.

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

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

Uri uri = Uri.parse("mailto:receipient@mail.com?subject=title&body=text");

相反.当然,文字 title text 应该是 Uri.encode().

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

这篇关于是否可以使用Uri.Builder而不使用"//"部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆