Java URL Query Build mapToQueryString(Map parameters, String charSet)

Here you can find the source of mapToQueryString(Map parameters, String charSet)

Description

map To Query String

License

Apache License

Declaration

public static String mapToQueryString(Map parameters, String charSet) 

Method Source Code

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

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;

import java.net.URLEncoder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Main {
    public static String mapToQueryString(Map parameters, String charSet) {
        String queryString = "";
        if (parameters != null && !parameters.isEmpty()) {
            Set<Entry> entrySet = parameters.entrySet();
            for (Entry entry : entrySet) {
                try {
                    String key = entry.getKey().toString();
                    Object value = entry.getValue();
                    List values = makeStringList(value);
                    for (Object v : values) {
                        queryString += key + "=" + URLEncoder.encode(v == null ? "" : v.toString(), charSet) + "&";
                    }// w w  w  .  ja  va 2s .c o m
                } catch (UnsupportedEncodingException e) {
                    throw new IllegalArgumentException("invalid charset : " + charSet);
                }
            }
            if (queryString.length() > 0) {
                queryString = queryString.substring(0, queryString.length() - 1);
            }
        }
        return queryString;
    }

    private static List<String> makeStringList(Object value) {
        if (value == null) {
            value = "";
        }
        List<String> result = new ArrayList<String>();
        if (value.getClass().isArray()) {
            for (int j = 0; j < Array.getLength(value); j++) {
                Object obj = Array.get(value, j);
                result.add(obj != null ? obj.toString() : "");
            }
            return result;
        }

        if (value instanceof Iterator) {
            Iterator it = (Iterator) value;
            while (it.hasNext()) {
                Object obj = it.next();
                result.add(obj != null ? obj.toString() : "");
            }
            return result;
        }

        if (value instanceof Collection) {
            for (Object obj : (Collection) value) {
                result.add(obj != null ? obj.toString() : "");
            }
            return result;
        }

        if (value instanceof Enumeration) {
            Enumeration enumeration = (Enumeration) value;
            while (enumeration.hasMoreElements()) {
                Object obj = enumeration.nextElement();
                result.add(obj != null ? obj.toString() : "");
            }
            return result;
        }
        result.add(value.toString());
        return result;
    }
}

Related

  1. getQueryString(String url)
  2. getQueryUrl(String word)
  3. getQueryUrl(String word, Integer page)
  4. getQueryURL(URL baseUrl, String[] parameters)
  5. getQueryValues(String input)
  6. mapToQueryString(Map parameters, String charSet)
  7. mapToQueryString(Map queryString)
  8. toQueryString(Map map)
  9. toQueryString(Map requestMap)