kohhworlwide kohhworlwide - 7 months ago 62
HTTP Question

String formulation in Java

I need to compose an URL string in javaME (which doesn't allow me to work with many libraries which would do this in a no time) like this

url = "http://helloworld.com/index.php?data={\"Word\":"+wordX+",\"RS\":20/04/13-2008:31:44,\"SER\":"+net2+"}";

This is producion a string like this:


The thing is I need the values to also be inside commas. I have tried everything but I'm not being able to understand how to do it.
Can someone explain this?



Create a MessageFormat:

final MessageFormat urlFormat = new MessageFormat("http://helloworld.com/index.php?data={\"Word\":\"{0}\",\"RS\":20/04/13-2008:31:44,\"SER\":\"{1}\"}");

Then simply apply your values to the format:

final String url = urlFormat.format(new Object[]{wordX, net2});

Or, create a format String:

final String urlFormat = "http://helloworld.com/index.php?data={\"Word\":\"%s\",\"RS\":20/04/13-2008:31:44,\"SER\":\"%s\"}";

And use String.format:

final String url = String.format(urlFormat, wordX, net2);

Or, just change you String to:

url = "http://helloworld.com/index.php?data={\"Word\":\""+wordX+"\",\"RS\":20/04/13-2008:31:44,\"SER\":\""+net2+"\"}";