Take a base url and a {@link Map} of parameters to build a valid url
import java.util.Map;
class Main {
/**
* Take a base url and a {@link Map} of parameters to build a valid url (eg
* : http://example.com?param1=value1¶m2=value2)
*
* @param baseUrl
* the base url (eg : http://example.com)
* @param parameters
* the {@link Map} of parameters (eg : {param1=value1,
* param2=value2})
* @return the builded url
*/
public static String buildUrl(String baseUrl, Map<String, Object> parameters) {
StringBuilder strBuilderUrl = new StringBuilder(baseUrl);
if (parameters != null && parameters.size() > 0) {
int i = 0;
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
if (i == 0) {
strBuilderUrl.append('?');
} else {
strBuilderUrl.append('&');
}
strBuilderUrl.append(entry.getKey());
strBuilderUrl.append('=');
strBuilderUrl.append(entry.getValue());
i++;
}
}
return strBuilderUrl.toString();
}
}
Related examples in the same category