Here you can find the source of getQueryString(final Map
Parameter | Description |
---|---|
baseUrl | The baseUrl. |
parameters | The additional parameters to add to the query string. |
public static String getQueryString(final Map<String, ? extends Object> parameters)
//package com.java2s; /*//www . j a va 2s .c om * Copyright 2004-2005 Revolution Systems Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Collection; import java.util.Map; import java.util.Map.Entry; public class Main { /** * Construct a new new URL from the baseUrl with the additional query string * parameters. * * @param baseUrl The baseUrl. * @param parameters The additional parameters to add to the query string. * @return The new URL. */ public static String getQueryString(final Map<String, ? extends Object> parameters) { final StringBuilder query = new StringBuilder(); appendQuery(query, parameters); return query.toString(); } public static void appendQuery(final StringBuilder query, final Map<String, ? extends Object> parameters) throws Error { if (parameters != null) { boolean firstParameter = true; for (final Entry<String, ? extends Object> parameter : parameters.entrySet()) { final String name = parameter.getKey(); final Object value = parameter.getValue(); if (name != null && value != null) { if (!firstParameter) { query.append('&'); } else { firstParameter = false; } try { if (value instanceof String[]) { final String[] values = (String[]) value; for (int i = 0; i < values.length; i++) { query.append(name).append('=').append(URLEncoder.encode(values[i], "US-ASCII")); if (i < values.length - 1) { query.append('&'); } } } else if (value instanceof Collection) { boolean first = true; final Collection<?> values = (Collection<?>) value; for (final Object childValue : values) { if (childValue != null) { if (first == true) { first = false; } else { query.append('&'); } query.append(name).append('=') .append(URLEncoder.encode(childValue.toString(), "US-ASCII")); } } } else { query.append(name).append('=').append(URLEncoder.encode(value.toString(), "US-ASCII")); } } catch (final UnsupportedEncodingException e) { throw new Error(e); } } } } } public static void appendQuery(final StringBuilder query, final String name, final Object value) { final boolean firstParameter = query.length() == 0; if (name != null && value != null) { if (!firstParameter) { query.append('&'); } try { if (value instanceof String[]) { final String[] values = (String[]) value; for (int i = 0; i < values.length; i++) { query.append(name).append('=').append(URLEncoder.encode(values[i], "US-ASCII")); if (i < values.length - 1) { query.append('&'); } } } else if (value instanceof Collection) { boolean first = true; final Collection<?> values = (Collection<?>) value; for (final Object childValue : values) { if (childValue != null) { if (first == true) { first = false; } else { query.append('&'); } query.append(name).append('=') .append(URLEncoder.encode(childValue.toString(), "US-ASCII")); } } } else { query.append(name).append('=').append(URLEncoder.encode(value.toString(), "US-ASCII")); } } catch (final UnsupportedEncodingException e) { throw new Error(e); } } } }