Here you can find the source of buildQuery(Map
Parameter | Description |
---|---|
paramMap | a parameter |
public static String buildQuery(Map<String, Object> paramMap)
//package com.java2s; /**//w ww . j a v a 2s . co m * Copyright 2010 Bing Ran<bing_ran@gmail.com> * * 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.List; import java.util.Map; public class Main { /** * * build valid http request querystrings from a hashmap that contains name value pairs * * Copied from Play Router class * * TODO: array support * * @param paramMap * @return */ public static String buildQuery(Map<String, Object> paramMap) { StringBuilder queryString = new StringBuilder(); for (Map.Entry<String, Object> entry : paramMap.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (value != null) { if (List.class.isAssignableFrom(value.getClass())) { List<Object> vals = (List<Object>) value; for (Object e : vals) { try { queryString.append(URLEncoder.encode(key, "utf-8")); queryString.append("="); if (e.toString().startsWith(":")) { queryString.append(e.toString() + ""); } else { queryString.append(URLEncoder.encode(e.toString() + "", "utf-8")); } queryString.append("&"); } catch (UnsupportedEncodingException ex) { } } } else { try { queryString.append(URLEncoder.encode(key, "utf-8")); queryString.append("="); if (value.toString().startsWith(":")) { queryString.append(value.toString() + ""); } else { queryString.append(URLEncoder.encode(value.toString() + "", "utf-8")); } queryString.append("&"); } catch (UnsupportedEncodingException ex) { } } } } String qs = queryString.toString(); if (qs.endsWith("&")) { qs = qs.substring(0, qs.length() - 1); } return qs; } }