Vaisakh N Vaisakh N - 1 year ago 176
Java Question

Extract parameter values from url android

public static Hashtable parseUrlString(String url) {
Hashtable parameter = new Hashtable();
List<NameValuePair> params = null;
try {
params = URLEncodedUtils.parse(new URI(url), "UTF-8");
for (NameValuePair param : params) {
if (param.getName() != null && param.getValue() != null)
parameter.put(param.getName(), param.getValue());
} catch (URISyntaxException e) {
return parameter;

The above method works for me to extract parameter name with value to a hashtable but in Api 21+ the NameValuePair and URLEncodedUtils is deprecated so what is the best way that i can replace this method?

Answer Source

In Android, you should never use URLEncodedUtils which is now deprecated. A good alternative is to use the Uri class instead: retrieve the query parameter keys using getQueryParameterNames() then iterate on them and retrieve each value using getQueryParameter().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download