How to get parameters out of a url encoded string (not a URL) in java
I have a url开发者_运维问答 encoded string that is returned from an API I am using. I want to do something request.getParameter("paramname") on the string. What I'm looking for is something like str.request.getParameter("paramname"). There's gotta be something like this right?
clarification the api returns something like: name1=val1&name2=val2&name3=val3
I know i could do a split on "&" and then go through each element but that seems stupid. Please advise. Thanks!
Use URLEncodedUtils from Apache httpclient library.
API : http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html
You will have to call this method to get name value pairs:
static List<NameValuePair> parse(HttpEntity entity)
Returns a list of NameValuePairs as parsed from an HttpEntity.
See this question
One answer there:
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");
精彩评论