Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get parameter names collection from Java/Android url

It's absolutely strange, but I can't find any Java/Android URL parser that will be compatible to return full list of parameters.

I've found java.net.URL and android.net.Uri but they are can't return parameters collection.

I want to pass url string, e.g.

String url = "http://s3.amazonaws.com/?AWSAccessKeyId=123&Policy=456&Signature=789&key=asdasd&Content-Type=text/plain&acl=public-read&success_action_status=201";

SomeBestUrlParser parser = new SomeBestUrlParser(url);
String[] parameters = parser.getParameterNames();
// should prints array with following elements
// AWSAccessKeyId, Policy, Signature, key, Content-Type, acl, success_action_status

Does anyone know ready solution?

like image 601
olegflo Avatar asked Oct 04 '12 13:10

olegflo


3 Answers

There is way to get collection of all parameter names.

String url = "http://domain.com/page?parameter1=value1&parameter2=value2";
List<NameValuePair> parameters = URLEncodedUtils.parse(new URI(url));
for (NameValuePair p : parameters) {
    System.out.println(p.getName());
    System.out.println(p.getValue());
}
like image 52
Olegykz Avatar answered Sep 17 '22 20:09

Olegykz


This static method builds map of parameters from given URL

private Map<String, String> extractParamsFromURL(final String url) throws URISyntaxException {
    return new HashMap<String, String>() {{
        for(NameValuePair p : URLEncodedUtils.parse(new URI(url), "UTF-8")) 
            put(p.getName(), p.getValue());
    }};
}

usage

extractParamsFromURL(url).get("key")

like image 23
ruX Avatar answered Sep 21 '22 20:09

ruX


Have a look at URLEncodedUtils

like image 38
Emil Avatar answered Sep 20 '22 20:09

Emil