Java URL Create constructURLString(Map parameters)

Here you can find the source of constructURLString(Map parameters)

Description

Expand the map of parameters to construct a URL string

License

Open Source License

Parameter

Parameter Description
parameters a parameter

Declaration

private static String constructURLString(Map<String, String> parameters) 

Method Source Code

//package com.java2s;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.Map;

public class Main {
    /**/*  w w w  .j av  a 2 s. co m*/
     * Expand the map of parameters to construct a URL string
     * 
     * @param parameters
     * @return
     */
    private static String constructURLString(Map<String, String> parameters) {
        StringBuffer url = new StringBuffer();

        boolean first = true;

        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            try {
                // type checking, we can get nulls in here
                if ((entry.getValue() == null) || (entry.getKey() == null)) {
                    continue;
                }
                if (entry.getValue().length() == 0) {
                    continue;
                }

                if (first) {
                    first = false;
                } else {
                    url.append("&");
                }
                url.append(URLEncoder.encode(entry.getKey(), "UTF-8") + "="
                        + URLEncoder.encode(entry.getValue(), "UTF-8"));
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
                // No Fixing this, really
                throw new Error("Unsupported Encoding Exception", ex);
            }
        }

        return url.toString();
    }
}

Related

  1. concatenateURL(URL url, String query)
  2. concatUrl(final URL baseUrl, final String extraPath)
  3. concaturl(final URL p_base, final String p_string)
  4. constructURL(URL base, String url, boolean stripRef)
  5. constructURLQueryString(Map urlParameters)
  6. convertToURL(String host)
  7. convertToURL(String url)
  8. convertToUrl(String[] paths)
  9. convertToURLs(String[] hosts)