code: public static String urlEncodeQuery(String str) {
          try {
              String encodeForForm = URLEncoder.encode(str, "UTF-8");
              return encodeForForm;
          } 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 oly be used for
  the query part of the URL, for other parts, like path, use {@link #urlEncode(String)}.
  
  @param str non-query string to encode
  @return encoded string that can be placed inside a URL