Java URL Build buildQuery(final Map query)

Here you can find the source of buildQuery(final Map query)

Description

Turns the specified Map into an encoded & escaped query

License

Open Source License

Parameter

Parameter Description
query Map to convert into a text based query

Return

Resulting query.

Declaration

public static String buildQuery(final Map<String, Object> query) 

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 {
    /**/*from w  w  w.  j a  v  a  2 s.c  o m*/
     * Turns the specified Map into an encoded & escaped query
     *
     * @param query Map to convert into a text based query
     * @return Resulting query.
     */
    public static String buildQuery(final Map<String, Object> query) {
        if (query == null) {
            return "";
        }
        final StringBuilder builder = new StringBuilder();

        for (final Map.Entry<String, Object> entry : query.entrySet()) {
            if (builder.length() > 0) {
                builder.append('&');
            }

            try {
                builder.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            } catch (final UnsupportedEncodingException e) {
            }

            if (entry.getValue() != null) {
                builder.append('=');
                try {
                    builder.append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
                } catch (final UnsupportedEncodingException e) {
                }
            }
        }

        return builder.toString();
    }
}

Related

  1. buildQuery(final Map query)
  2. buildQuery(Map paramMap)
  3. buildQuery(Map paramMap)
  4. buildQuery(Map query)