简体   繁体   中英

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

I'm trying to store a fully qualified url, with also query params:

www.miosito.net?prova&reg=bis

but it's causing a problem because &reg is similar to ® entity and android tell me that and html entity is not well written.

I need this because every locale uses a fully different set of url query param.

I tried with [[CDATA[.. ]] but this syntax disliked by xml parser.

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:

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

Store it like this:

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

Where &amp; is the XML equivelant of the ampersand symbol & .

百分比编码可以起到作用: 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.

Update 1 : Have a look at the following link for more info :

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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