Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parameters values from a URL

I have an issue in getting parameters from a URL mentioned below,

String url = http://example.com/api_callback#access_token=XXXXXXXXXXXXXXX&state=enabled&scope=profile%20booking&token_type=bearer&expires_in=15551999

My code to extract the parameters is as follows:

Uri uri = Uri.parseurl(url); 
Set<String> paramNames = uri.getQueryParameterNames();

However, as you can see a "#" in the URL instead of "?" so that's why I am not able to get the parameters Set.

First thing that came to my mind is to replace "#" with "?" using String.replace method then I thought their might be better solution for this. So if you guys have better solution please help me.

like image 433
Ishpreet Sahil Babber Avatar asked Oct 27 '25 18:10

Ishpreet Sahil Babber


1 Answers

Easiest method:

 String string = url.replace("#","?");
 String access_token = Uri.parse(string).getQueryParameter("access_token");

 Log.d("TAG", "AccessToken: " + access_token);

Now you can get any parameter from the url just by passing their name.

Good Luck

like image 130
Tu2l Avatar answered Oct 30 '25 08:10

Tu2l