1

In my android project I have a string resource (string.xml) in the res/ folder.

in that string.xml I have created several strings, like:

<string name="my_server">http://12.34.56.78</string>
<string name="my_port">1234</string>

then I use in the code:

String url = R.strings.my_server + ":" + R.strings.my_port;
System.out.println("My server and port is: " + url);

but my output is:

My server and port is : 2131099700:2131099702

Where is my string? That seem kind a adress of that string?

Ralf Wickum
  • 2,850
  • 9
  • 55
  • 103
  • Possible duplicate of [Eclipse/Java - Values in R.string.\* return int?](http://stackoverflow.com/questions/1983548/eclipse-java-values-in-r-string-return-int) – Selvin Dec 04 '15 at 12:04

5 Answers5

3

Change :

<string name="my_server">http://12.34.56.78</string>
...

String url = R.strings.my_server + ":" + R.strings.my_port;

to

<string name="my_server">http:\/\/12.34.56.78</string>
...

String url = getString(R.strings.my_server) + ":" + getString(R.strings.my_port);

You needed to escape the my_server string, plus use the getString(int) method to retrieve the strings.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • Nope, when I just have the server without port : String url = R.strings.my_server; // Android studio complains: required String, found int. Offers to use String.valueOf() – Ralf Wickum Dec 04 '15 at 11:49
  • log4j:WARN No appenders could be found for logger (com.example.json.JacksonRequest). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. – Ralf Wickum Dec 04 '15 at 11:55
  • I see that this error has something to do with the **Jackson** parser you are using, not this. – Mohammed Aouf Zouag Dec 04 '15 at 11:56
1

You can get the String value of a string resource like this for example:

String yourString = context.getResources().getString(R.string.my_port);

where context is the context of your activity

or like this if you import import android.content.res.Resources:

String yourString = Resources.getSystem().getString(R.string.my_port);
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
0

It seems like you are actually refering to a Id in your R File not to the Value connected with it.

You need to do that in order to get the String:

getResources().getString(R.string.my_server)

According to the Android Documentation you don't need to escape '/'.

B. Kemmer
  • 1,517
  • 1
  • 14
  • 32
0

R.string will return the id of that particular string in form of an Integer.

To get actual string, you will use getResources().getString(R.string.your_string);

So, your url string will be like this

String url = getResources().getString(R.strings.my_server) + ":" + getResources().getString(R.strings.my_port);
Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
0

Just want to add that you need to use context and getString method:

context.getString(R.id.your_string_name)

For example in my adapter class, I defines the Context as

private Context mContext;

Then I use it in the message section of alert dialog as

...

.setMesasge(mContext.getString(R.string.my_string_name))

...
Greenonline
  • 1,330
  • 8
  • 23
  • 31