如何从java中的url编码字符串(而不是URL)中获取参数
我有一个从我正在使用的API返回的url编码字符串。我想在字符串上做一些request.getParameter(paramname)。我正在寻找的是像str.request.getParameter(paramname)。有这样的东西吗?
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?
澄清api返回的内容如下:
name1 = val1& name2 = val2& name3 = val3
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!
使用Apache httpclient库中的URLEncodedUtils。
Use URLEncodedUtils from Apache httpclient library.
你会必须调用此方法来获取名称值对:
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.