kohhworlwide kohhworlwide - 1 month ago 16
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:

http://helloworld.com/index.php?data={"Word":358741051173885,"RS":20/04/13-2008:31:44,"SER":53543d303b44523d4e616fd}


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?

Thanks

Answer

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+"\"}"; 
Comments