Search code examples
javaurl-encoding

URLEncoder for integer value


int a=50;

How do I encode this using URLEncoder?

For strings we do

String value=URLEncoder.encoder("SomeStringValye",this.encoding);

Solution

  • Integer numbers in the URL are not an issue. You need not URL encode it. So in your case, simply construct a url by concatenating as follows

    String s = "www.xyz.com/?id=" +1; 
    

    If you have some special characters in your url parameter like space, ;, then you have to url encode the parameter value

    URLEncoder.encode(
    "urlParameterString",
    java.nio.charset.StandardCharsets.UTF_8.toString() )
    

    If you still want to pass the integer to the URL encoder method, simply make your integer as a string e.g., 10+""