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?
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()
.
You can use the android.net.Uri.
Uri uri=Uri.parse(url);
It has getQueryParameterNames() and uri.getQueryParameter(parameterNameString);
try:
Uri uri = Uri.parse(yourStrUrl);
String paramValue = uri.getQueryParameter("yourParam");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With