Java URL Build buildQuery(Map query)

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

Description

build Query

License

Open Source License

Declaration

public static String buildQuery(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.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static String buildQuery(Map<String, Object> query) {
        if (query == null) {
            return "";
        } else {//from ww  w . j  av  a 2s .c om
            StringBuilder builder = new StringBuilder();
            Iterator<Entry<String, Object>> it = query.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, Object> entry = it.next();
                if (builder.length() > 0) {
                    builder.append("&");
                }

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

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

            return builder.toString();
        }
    }
}

Related

  1. buildQuery(final Map query)
  2. buildQuery(final Map query)
  3. buildQuery(Map paramMap)
  4. buildQuery(Map paramMap)
  5. buildQuery(Map params)
  6. buildQueryString(Map parameters)
  7. buildQueryString(Map params)
  8. buildQueryString(Map queryParams)