Java URL Build buildQueryString(Multimap params)

Here you can find the source of buildQueryString(Multimap params)

Description

Create a query string

License

Apache License

Declaration

private static String buildQueryString(Multimap<String, String> params) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.String.format;
import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;
import java.util.Map;
import com.google.common.collect.Multimap;

public class Main {
    /**// w  w w .  j  av  a2s .  co  m
     * Create a query string
     */
    private static String buildQueryString(Multimap<String, String> params) {
        StringBuilder bld = new StringBuilder();

        boolean afterFirst = false;
        for (Map.Entry<String, String> entry : params.entries()) {
            if (afterFirst)
                bld.append("&");
            else
                afterFirst = true;

            bld.append(urlEncode(entry.getKey()));
            bld.append("=");
            checkNotNull(entry.getValue(), format("query parameter[%s] has no value", entry.getKey()));
            bld.append(urlEncode(entry.getValue()));
        }

        return bld.toString();
    }

    /**
     * An interface to URLEncoder.encode() that isn't inane
     */
    public static String urlEncode(Object value) {
        try {
            return URLEncoder.encode(value.toString(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. buildQuery(Map params)
  2. buildQueryString(Map parameters)
  3. buildQueryString(Map params)
  4. buildQueryString(Map queryParams)
  5. buildQueryString(Map params)
  6. buildURL(int imageId)
  7. buildUrl(String base, String local)
  8. buildUrl(String cid, String lid, String sid, String pid, String qid, String imageName)
  9. buildURL(String host, int port, String base, String ext)