Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to call a URL from Java code in the following way:

userId = "Ankur";
template = "HelloAnkur";
value= "ParamValue";
String urlString = "https://graph.facebook.com/" + userId + "/notifications?template=" +
    template + "&href=processThis.jsp?param=" + value + "&access_token=abc123";

I have the following problems in this:

  1. When I do println(urlString), I see that the urlString only has the value upto and before the first ampersand (&). That is, it looks as: https://graph.facebook.com/Ankur/notifications?template=HelloAnkur and rest of it all (which should have been &href=processThis.jsp?param=ParamValue&access_toke=abc123) gets cut off. Why is that and how can I get and keep the full value in urlString? Does & needs to be escaped in a Java String, and if yes, how to do it?
  2. Notice that I am trying to pass a (relative) URL as a parameter value in this query (the value of href as processThis.jsp?param=ParamValue. How can I pass this type of value of href without mixing it up with the query of this URL (urlString), which only has three parameters template, href and access_token? That is, how can I hide or escape ? and =? Further, what would I need to do if value was Param Value (with a space)?
  3. Notice that the template has the value HelloAnkur (with no space). But if I wanted it to have space, as in Hello Ankur, how would I do it? Should I write it as Hello%20Ankur or Hello Ankur would be fine?
  4. I need the solution in such a way that URL url = new URL(urlString) can be created, or url can be created via URI. Please describe your answer up to this point as creating a safe URL is not straight forward in Java.

Thanks!

share|improve this question
    
I don't use Java, but there's this link that talks about URLEncoding in Java, which is what you need for your URI I think: stackoverflow.com/questions/10786042/java-url-encoding –  dKen Jun 6 '13 at 16:21
    
@dKen I tried this, it did not work for me. My first problem here is that the URL String gets truncated at the first &, and I don't know why. Your reference talks about encoding the query parameters' values. That is my later problem. –  Ankur Jun 6 '13 at 19:53

1 Answer 1

up vote 1 down vote accepted

(this is going to become a classic)

Use URI Templates (RFC 6570). Using this implementation (disclaimer: mine), you can avoid all encoding problems altogether:

// Immutable, can be reused as many times as you wish
final URITemplate template = new URITemplate("https://graph.facebook.com/{userId}"
    + "/notifications?template={template}"
    + "&href=processThis.jsp?param={value}"
    + "&access_token=abc123");

final Map<String, VariableValue> vars = new HashMap<String, VariableValue>();

vars.put("userId", new ScalarValue("Ankur"));
vars.put("template", new ScalarValue("HelloAnkur"));
vars.put("value", new ScalarValue("ParamValue");

// Build the expanded string
final String expanded = template.expand(vars);

// Go with the string

Note that URI templates not only allow scalar values, but also arrays (the RFC calls these "lists" -- implemented as ListValue in the above) and maps (the RFC calls these "associative arrays" -- implemented as MapValue in the above).

share|improve this answer
    
How can I use your implementation -- will I have to import a package so that URITemplate can be recognized? Will I have to download something or can I copy-paste the code in some way in my project? BTW, I am using Eclipse and my code is deployed on Google App Engine. I would appreciate if you could provide directions. Thanks. –  Ankur Jun 6 '13 at 19:30
    
Do you use maven in your project? –  fge Jun 6 '13 at 19:31
    
I forgot: you can just download it from Maven central, see here. Note: Guava is also needed as a dependency. Other jars (testng, jackson-coreutils) are only needed for tests –  fge Jun 6 '13 at 19:34
    
No, I don't use Maven or Guava. (I don't know what it is, and how it can be used with Eclipse and Google App Engine). –  Ankur Jun 6 '13 at 19:56
    
No need for maven especially. But you'll need to download Guava. Follow the links to it in the link above. –  fge Jun 6 '13 at 19:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.