How to construct a URI with parameters using URIBuilder?

zeromem picture zeromem · Jul 19, 2017 · Viewed 11.3k times · Source

How to construct a URI like www.google.com/p1/p2?what&k1=v1&k2=v2 using URIBuilder ? When I use Java's URIBuilder.setParameter(), I must pass in an name and a value. How can I construct the above uri without using URIBuilder.setCustomQuery() ?

Answer

Aakash Verma picture Aakash Verma · Jul 19, 2017
UriBuilder builder = new UriBuilder()
    .setScheme("https")
    .setHost("www.google.com")
    .setPath("/p1/p2")
    .addParameter("what",null)
    .addParameter("k1", "v1")
    .addParameter("k2", "v2")
builder.toString();