code: public static String encodeQuery(String query) {
          if (query == null) {
              return null;
          }
  
          StringBuilder rebuiltQuery = new StringBuilder();
  
          // Encode parameters to mitigate XSS attacks
          String[] queryParams = query.split("&");
          for (String param : queryParams) {
              String rebuiltParam = encode(param);
              int equalIndex = param.indexOf("=");
              if (equalIndex > -1) {
                  String name = param.substring(0, equalIndex);
                  String value = (equalIndex < (param.length() - 1)) ? param.substring(equalIndex + 1) : "";
                  rebuiltParam = encode(name) + "=" + encode(value);
              }
              if (!rebuiltParam.isEmpty()) {
                  rebuiltQuery.append(rebuiltParam + "&");
              }
          }
          // Remove trailing '&' character
          if (rebuiltQuery.length() > 0 && rebuiltQuery.charAt(rebuiltQuery.length() - 1) == '&') {
              rebuiltQuery.deleteCharAt(rebuiltQuery.length() - 1);
          }
          return rebuiltQuery.toString();
      }
docs: Encodes each parameter in the provided query. Expects the query argument to be the query string of a URL with parameters
  in the format: param=value(&param2=value2)*
  
  @param query
  @return