final String decodedParamVal = URLDecoder.decode(paramVal, utf8.name()); list.add(new BasicNameValuePair(decodedParamName, decodedParamVal)); } else { final String key = URLDecoder.decode(parameterCombo[0], utf8.name()); final String val = parameterCombo.length == 2 ? URLDecoder.decode(parameterCombo[1], utf8.name()) : ""; list.add(new BasicNameValuePair(key, val));
/** * Sets parameter of URI query overriding existing value if set. The parameter name and value * are expected to be unescaped and may contain non ASCII characters. * <p> * Please note query parameters and custom query component are mutually exclusive. This method * will remove custom query if present. * </p> */ public URIBuilder setParameter(final String param, final String value) { if (this.queryParams == null) { this.queryParams = new ArrayList<BasicNameValuePair>(); } if (!this.queryParams.isEmpty()) { for (final Iterator<BasicNameValuePair> it = this.queryParams.iterator(); it.hasNext(); ) { final BasicNameValuePair nvp = it.next(); if (nvp.getName().equals(param)) { it.remove(); } } } this.queryParams.add(new BasicNameValuePair(param, value)); this.encodedQuery = null; this.encodedSchemeSpecificPart = null; this.query = null; return this; }
/** * Adds parameter to URI query. The parameter name and value are expected to be unescaped * and may contain non ASCII characters. * <p> * Please note query parameters and custom query component are mutually exclusive. This method * will remove custom query if present. * </p> */ public URIBuilder addParameter(final String param, final String value) { if (this.queryParams == null) { this.queryParams = new ArrayList<BasicNameValuePair>(); } this.queryParams.add(new BasicNameValuePair(param, value)); this.encodedQuery = null; this.encodedSchemeSpecificPart = null; this.query = null; return this; }
@Test public void iterableQueryParameters() { List<URIBuilder.BasicNameValuePair> list = new ArrayList<URIBuilder.BasicNameValuePair>(); list.add(new URIBuilder.BasicNameValuePair("hello", "world")); list.add(new URIBuilder.BasicNameValuePair("hello", "monde")); URIBuilder builder = new URIBuilder() .setScheme("http") .setHost("apache.org") .setPath("/shindig") .addParameters(list) .setFragment("foo"); assertEquals("http://apache.org/shindig?hello=world&hello=monde#foo", builder.toString()); }