Android:如何将 url 存储在 string.xml 资源文件中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12237462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How store url in string.xml resource file?
提问by realtebo
I'm trying to store a fully qualified url, with also query params:
我正在尝试存储一个完全限定的 url,还有查询参数:
www.miosito.net?prova®=bis
but it's causing a problem because ®
is similar to ®
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 解析器不喜欢这种语法。
采纳答案by Marcin Orlowski
The problem is not with &req
but with &
itself. For XML/HTML you would have to use &
entity (or &
), but for URLs you should rather URL-encode (see docs) strings, and in that case said &
should be replaced with %26
. So your final string should look like:
问题不在于自身,&req
而在于&
自身。对于 XML/HTML,您必须使用&
entity(或&
),但对于 URL,您应该使用 URL 编码(请参阅文档)字符串,在这种情况下,&
应将 say 替换为%26
. 所以你的最终字符串应该是这样的:
www.miosito.net?prova%26reg=bis
www.miosito.net?prova%26reg=bis
回答by iTurki
Store it like this:
像这样存储它:
<string name="my_url">"www.miosito.net?prova&reg=bis"</string>
Where &
is the XML equivelant of the ampersand symbol&
.
&
与符号的 XML 等效项在哪里&
。
回答by sandyiscool
You can enclose your url in double quotes, something like :
您可以将您的网址用双引号括起来,例如:
<string name="my_url">"www.miosito.net?prova®=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. I tried out www.miosito.net?prova%26reg=bis
更新 2:@WebnetMobile.com:正确,确实是 :) '&' 被 xml 视为特殊字符,用引号括起来不起作用。我试过了 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),"?","&") ;
它也没有成功。我什至尝试将它用引号括起来,但仍然没有用。我错过了什么吗?
同时,以下确实有效:<string name="my_url">www.miosito.net%1$sprova%2$sreg=bis</string>
然后在代码中: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' 用于十进制数等等在。
回答by mehmetminanc
Percent encoding may do the trick: http://en.wikipedia.org/wiki/Percent-encodingYou'll basically have something like this: www.miosito.net?prova%26reg=bis
百分比编码可能 会起作用:http: //en.wikipedia.org/wiki/Percent-encoding你基本上会有这样的东西:www.miosito.net?prova%26reg=bis