Java URL Parameter Builder serializeParameters(final Map parameters)

Here you can find the source of serializeParameters(final Map parameters)

Description

serialize Parameters

License

Open Source License

Declaration

public static String serializeParameters(final Map<String, String> parameters) 

Method Source Code


//package com.java2s;
// Licensed under the MIT license. See sibling License.txt file

import java.io.UnsupportedEncodingException;

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

public class Main {
    public static final String UTF_8 = "UTF-8";

    public static String serializeParameters(final Map<String, String> parameters) {
        try {//from   w w  w .  j av  a  2s  .c o  m
            final StringBuilder sb = new StringBuilder();
            final Iterator<Map.Entry<String, String>> iterator = parameters.entrySet().iterator();
            if (iterator.hasNext()) {
                Map.Entry<String, String> entry;
                String key;
                String encodedKey;
                String value;
                String encodedValue;

                entry = iterator.next();
                key = entry.getKey();
                encodedKey = URLEncoder.encode(key, UTF_8);
                sb.append(encodedKey);
                value = entry.getValue();
                if (value != null) {
                    encodedValue = URLEncoder.encode(value, UTF_8);
                    sb.append('=').append(encodedValue);
                }
                while (iterator.hasNext()) {
                    sb.append('&');
                    entry = iterator.next();
                    key = entry.getKey();
                    encodedKey = URLEncoder.encode(key, UTF_8);
                    sb.append(encodedKey);
                    value = entry.getValue();
                    if (value != null) {
                        encodedValue = URLEncoder.encode(value, UTF_8);
                        sb.append('=').append(encodedValue);
                    }
                }
            }
            return sb.toString();
        } catch (final UnsupportedEncodingException e) {
            throw new Error(e);
        }
    }
}

Related

  1. parseGetParameters(HttpExchange exchange)
  2. parseParameters(final String value)
  3. parseResponseParams(String body)
  4. sendGetRequest(String path, Map params, String enc)
  5. sendPostRequest(String path, Map params, String enc)
  6. serializeParameters(final Map params)
  7. stringToMap(String input)
  8. stringToMap(String input)
  9. stringUncode(String param)