Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract parameter values from url android

Tags:

java

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) {
        e.printStackTrace();
    }
    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?

like image 686
Vaisakh N Avatar asked Jun 04 '15 09:06

Vaisakh N


3 Answers

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().

like image 170
BladeCoder Avatar answered Nov 15 '22 08:11

BladeCoder


You can use the android.net.Uri.

Uri uri=Uri.parse(url);

It has getQueryParameterNames() and uri.getQueryParameter(parameterNameString);

like image 26
Laurentiu L. Avatar answered Nov 15 '22 08:11

Laurentiu L.


try:

 Uri uri = Uri.parse(yourStrUrl);
 String paramValue = uri.getQueryParameter("yourParam");
like image 20
AITAALI_ABDERRAHMANE Avatar answered Nov 15 '22 08:11

AITAALI_ABDERRAHMANE