Java URL Query Build toQueryString(Map map)

Here you can find the source of toQueryString(Map map)

Description

to Query String

License

Open Source License

Declaration

public static String toQueryString(Map<?, ?> map) throws UnsupportedEncodingException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.Map;

public class Main {
    public static String toQueryString(Map<?, ?> map) throws UnsupportedEncodingException {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            if (sb.length() > 0) {
                sb.append("&");
            }//  w  ww . ja v  a  2  s .c  o m
            sb.append(String.format("%s=%s", urlEncodeUTF8(entry.getKey().toString()),
                    urlEncodeUTF8(entry.getValue().toString())));
        }
        return sb.toString();
    }

    static String urlEncodeUTF8(String s) throws UnsupportedEncodingException {
        return URLEncoder.encode(s, "UTF-8");
    }
}

Related

  1. getQueryURL(URL baseUrl, String[] parameters)
  2. getQueryValues(String input)
  3. mapToQueryString(Map parameters, String charSet)
  4. mapToQueryString(Map parameters, String charSet)
  5. mapToQueryString(Map queryString)
  6. toQueryString(Map requestMap)