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:
- When I do
println(urlString)
, I see that theurlString
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 inurlString
? Does&
needs to be escaped in a Java String, and if yes, how to do it? - Notice that I am trying to pass a (relative) URL as a parameter value in this query (the value of
href
asprocessThis.jsp?param=ParamValue
. How can I pass this type of value ofhref
without mixing it up with the query of this URL (urlString
), which only has three parameterstemplate
,href
andaccess_token
? That is, how can I hide or escape?
and=
? Further, what would I need to do ifvalue
wasParam Value
(with a space)? - Notice that the
template
has the valueHelloAnkur
(with no space). But if I wanted it to have space, as inHello Ankur
, how would I do it? Should I write it asHello%20Ankur
orHello Ankur
would be fine? - I need the solution in such a way that
URL url = new URL(urlString)
can be created, orurl
can be created viaURI
. Please describe your answer up to this point as creating a safe URL is not straight forward in Java.
Thanks!