Java tutorial
//package com.java2s; //License from project: Apache License import java.io.UnsupportedEncodingException; import java.net.URLEncoder; public class Main { private static String encodeUrlParams(/*@Nullable*/String userLocale, /*@Nullable*/String/*@Nullable*/[] params) { StringBuilder buf = new StringBuilder(); String sep = ""; if (userLocale != null) { buf.append("locale=").append(userLocale); sep = "&"; } if (params != null) { if (params.length % 2 != 0) { throw new IllegalArgumentException( "'params.length' is " + params.length + "; expecting a multiple of two"); } for (int i = 0; i < params.length;) { String key = params[i]; String value = params[i + 1]; if (key == null) throw new IllegalArgumentException("params[" + i + "] is null"); if (value != null) { buf.append(sep); sep = "&"; buf.append(encodeUrlParam(key)); buf.append("="); buf.append(encodeUrlParam(value)); } i += 2; } } return buf.toString(); } public static String encodeUrlParam(String s) { try { return URLEncoder.encode(s, "UTF-8"); } catch (UnsupportedEncodingException ex) { throw new AssertionError("UTF-8 should always be supported", ex); } } }