code: public static String urlEncode(String str) {
          try {
              String encodeForForm = URLEncoder.encode(str, "UTF-8");
              String encodeForUrl = encodeForForm.replace("+", "%20");
              return encodeForUrl;
          } catch (UnsupportedEncodingException e) {
              throw new IllegalStateException("JVM does not support UTF-8");
          }
      }
docs: Encodes the given string so it can be placed inside a URL. This should not be the query part of the URL, for
  that use {@link #urlEncodeQuery(String)}.
  
  @param str non-query string to encode
  @return encoded string that can be placed inside a URL