Java URL Create buildUrl(String url, HashMap params)

Here you can find the source of buildUrl(String url, HashMap params)

Description

build Url

License

Apache License

Declaration

private static String buildUrl(String url, HashMap<String, String> params) throws UnsupportedEncodingException 

Method Source Code

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

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class Main {
    private static String buildUrl(String url, HashMap<String, String> params) throws UnsupportedEncodingException {

        StringBuilder urlString = new StringBuilder(url);

        if (params.isEmpty()) {
            return urlString.toString();
        }//w  ww  .  ja va  2s.com

        urlString.append("?");

        Iterator<Entry<String, String>> it = params.entrySet().iterator();

        while (it.hasNext()) {
            Entry<String, String> entry = it.next();
            urlString.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            urlString.append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            urlString.append(it.hasNext() ? '&' : "");
        }

        return urlString.toString();
    }
}

Related

  1. buildURL(String spec)
  2. buildURL(String spec, boolean trailingSlash)
  3. buildUrl(String url)
  4. buildURL(String url)
  5. buildURL(String url)
  6. buildUrl(String URL, Map params)
  7. buildUrl(String urlPrefix, String urlSuffix)
  8. buildURL(URI base, Multimap params)
  9. buildUrlPath(String baseUrl, String childUrl)