Java examples for java.net:URL Query
Create URL query string from Map<String,String>
//package com.java2s; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Map; import java.util.Map.Entry; public class Main { private static final String DEFAULT_ENCODING = "UTF-8"; public static String query(Map<String, String> params) { return query(params, DEFAULT_ENCODING); }//from ww w .ja v a 2 s. c o m public static String query(Map<String, String> params, String encoding) { String query = ""; if (params != null && params.size() > 0) { StringBuilder sb = new StringBuilder(); boolean first = true; for (Entry<String, String> entry : params.entrySet()) { if (!first) { sb.append('&'); } else { first = false; } try { sb.append(URLEncoder.encode(entry.getKey(), encoding)); sb.append('='); sb.append(URLEncoder.encode(entry.getValue(), encoding)); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } if (sb.length() > 0) { query = sb.toString(); } } return query; } }