How to get parameters from url encoded string (not url) in java

I have a url encoded string that is returned from the API that I am using. I want to do something request.getParameter ("paramname") in a string. I am looking for something like str.request.getParameter ("paramname"). Should there be something like this right?

the api explanation returns something like: name1 = value1 & name2 = value2 & name3 = val3

I know that I can split into &, and then go through each element, but that seems silly. Please advise. Thank!

+5
source share
2 answers

Use the URLEncodedUtils from the Apache httpclient library.

API: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html

, :

static List<NameValuePair>  parse(HttpEntity entity)
          Returns a list of NameValuePairs as parsed from an HttpEntity.
+2

.

:

import org.eclipse.jetty.util.*;

MultiMap<String> params = new MultiMap<String>();
UrlEncoded.decodeTo("foo=bar&bla=blub", params, "UTF-8");

assert params.getString("foo").equals("bar");
assert params.getString("bla").equals("blub");
0

All Articles