All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.alipsa.simplerest.UrlParameters Maven / Gradle / Ivy

The newest version!
package se.alipsa.simplerest;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * Utility class to easily build url parameters.
 *
 * 
 * UrlParameters.parameters("foo", "123", "bar", "898")
 * 
 * will result in the parameter string "?foo=123&bar=898"
 *
 */
public class UrlParameters {

  /**
   *
   * @param params the key value pairs to build a parameter string from
   * @return a parameter string suitable to append to an url. Each parameter is properly url encoded.
   */
  public static String parameters(String... params) {
    if (params.length == 0) {
      return "";
    }
    String up = "?";
    int i = 0;
    for (String param : params) {
      i++;
      if (i % 2 != 0) {
        if (i == 1) {
          up = up + URLEncoder.encode(param, StandardCharsets.UTF_8);
        } else {
          up = up + "&" + URLEncoder.encode(param, StandardCharsets.UTF_8);
        }
      } else {
        up = up + "="+ URLEncoder.encode(param, StandardCharsets.UTF_8);
      }
    }
    return up;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy