public static String encodeQuery(String query) {
    try {
      return new URI(null, null, null, query, null).toASCIIString().substring(1);  // remove leading ?
    }
    catch (URISyntaxException e) {
      return query;
    }
  }
public static String encodeQuery(String query)