Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get parameters from URL in Android? [duplicate]

Tags:

java

android

I have a URL like this:

http://www.chalklit.in/post.html?chapter=V-Maths-Addition%20&%20Subtraction&post=394

How to get the value of parameter of chapter and post?

My URL contains '&' in the value of chapter parameter.

like image 992
Shubham Chauhan Avatar asked Nov 07 '16 13:11

Shubham Chauhan


People also ask

How do you get multiple parameters in a URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

How can I get parameters from a URL string?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.


1 Answers

You can use the Uri class in Android to do this; https://developer.android.com/reference/android/net/Uri.html

Uri uri = Uri.parse("http://www.chalklit.in/post.html?chapter=V-Maths-Addition%20&%20Subtraction&post=394"); String server = uri.getAuthority(); String path = uri.getPath(); String protocol = uri.getScheme(); Set<String> args = uri.getQueryParameterNames(); 

Then you can even get a specific element from the query parameters as such;

String chapter = uri.getQueryParameter("chapter");  //will return "V-Maths-Addition " 
like image 175
jason.kaisersmith Avatar answered Oct 09 '22 19:10

jason.kaisersmith