Java URL Encode encode(HashMap map)

Here you can find the source of encode(HashMap map)

Description

encode

License

Open Source License

Declaration

public static String encode(HashMap<String, Object> map) 

Method Source Code

//package com.java2s;
/* /*from w  ww .j  av  a  2 s .co m*/
 * Copyright (C) 2014 Peter Cai
 *
 * This file is part of BlackLight
 *
 * BlackLight is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * BlackLight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with BlackLight.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Set;

public class Main {
    public static String encode(HashMap<String, Object> map) {
        if (map == null) {
            return null;
        }
        StringBuilder str = new StringBuilder();
        Set<String> keys = map.keySet();
        boolean first = true;

        for (String key : keys) {
            Object value = map.get(key);

            if (first) {
                first = false;
            } else {
                str.append("&");
            }

            try {
                str.append(URLEncoder.encode(key, "UTF-8")).append("=")
                        .append(URLEncoder.encode(value.toString(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return str.toString();
    }
}

Related

  1. encode(final String content, final String encoding)
  2. encode(final String raw)
  3. encode(final String s, final String enc)
  4. encode(final String string)
  5. encode(final String value, final String charset)
  6. encode(Object parameter)
  7. encode(Object value)
  8. encode(Properties prop)
  9. encode(String _string, String _sEncoding)