简体   繁体   English

Android:如何在string.xml资源文件中存储url?

[英]Android: How store url in string.xml resource file?

I'm trying to store a fully qualified url, with also query params: 我正在尝试存储一个完全限定的URL,还有查询参数:

www.miosito.net?prova&reg=bis

but it's causing a problem because &reg is similar to ® 但它引起了一个问题,因为&reg类似于® entity and android tell me that and html entity is not well written. 实体和android告诉我,html实体写得不好。

I need this because every locale uses a fully different set of url query param. 我需要这个,因为每个语言环境都使用一组完全不同的url查询参数。

I tried with [[CDATA[.. ]] but this syntax disliked by xml parser. 我尝试了[[CDATA[.. ]]但xml解析器不喜欢这种语法。

The problem is not with &req but with & itself. 问题不在于&req而在于&本身。 For XML/HTML you would have to use & 对于XML / HTML,您必须使用& entity (or & ), but for URLs you should rather URL-encode ( see docs ) strings, and in that case said & should be replaced with %26 . 实体(或& ),但对于URL,您应该使用URL编码( 请参阅文档 )字符串,在这种情况下&应该用%26替换。 So your final string should look like: 所以你的最终字符串应如下所示:

www.miosito.net?prova%26reg=bis www.miosito.net?prova%26reg=bis

Store it like this: 像这样存储:

<string name="my_url">"www.miosito.net?prova&amp;reg=bis"</string>

Where &amp; 哪里&amp; is the XML equivelant of the ampersand symbol & . &符号 &的XML等价物。

百分比编码可以起到作用: http//en.wikipedia.org/wiki/Percent-encoding你基本上会有这样的东西:www.miosito.net?prova%26reg =bis

You can enclose your url in double quotes, something like : 您可以将您的网址用双引号括起来,例如:

<string name="my_url">"www.miosito.net?prova&reg=bis"</string>

This is a recommended way to enclose string resources in Android. 这是在Android中包含字符串资源的推荐方法。

Update 1 : Have a look at the following link for more info : 更新1:有关详细信息,请查看以下链接:

http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

Update 2: @WebnetMobile.com : Correct, indeed :) '&' is being treated a special character by xml and enclosing in quotes doesn't work. 更新2:@WebnetMobile.com:正确,确实:)'&'被xml视为特殊字符,并且用引号括起来不起作用。 I tried out 我试过了
www.miosito.net?prova%26reg=bis

and it didn't work out either. 它也没有成功。 I even tried enclosing it in quotes but still didn't work. 我甚至尝试用引号括起来,但仍然没有用。 Am I missing something ? 我错过了什么吗?
Meanwhile, the following does work : 同时,以下工作:

<string name="my_url">www.miosito.net%1$sprova%2$sreg=bis</string>

and then in code : 然后在代码中:

Resources resources=getResources();
String url=String.format(resources.getString(R.string.my_url),"?","&") ;

The '%1$s' and '%2$s' are format specifiers, much like what is used in printf in C. '%1$s' is for strings, '%2$d' is for decimal numbers and so on. '%1 $ s'和'%2 $ s'是格式说明符,非常类似于在C.中的printf中使用的''%1 $ s'用于字符串,'%2 $ d'用于十进制数字,因此上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM