Ishpreet Sahil Babber
Ishpreet Sahil Babber

Reputation: 41

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.

Upvotes: 4

Views: 10827

Answers (4)

Tu2l
Tu2l

Reputation: 61

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

Upvotes: 6

Bharat Vasoya
Bharat Vasoya

Reputation: 361

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("key");

Upvotes: 1

Rohan Ashik
Rohan Ashik

Reputation: 83

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");
//Then you can even get a specific element from the query parameters as such;
String chapter = uri.getQueryParameter("chapter");  //will return "V-Maths-Addition "

Upvotes: 2

Zain Ul Abidin
Zain Ul Abidin

Reputation: 2710

'#' is called refrence parameter, Here you can do one of two things either replace the '#' with '?' and process your uri i.e

String url = "http://example.com/api_callback#access_token=XXXXXXXXXXXXXXX&state=enabled&scope=profile%20booking&token_type=bearer&expires_in=15551999";
url = url.Replace("#","?"); //now your URI object to proceed further

or other alternative

String url = "http://example.com/api_callback#access_token=XXXXXXXXXXXXXXX&state=enabled&scope=profile%20booking&token_type=bearer&expires_in=15551999";
URL myurl = new URL(url);
String refrence = myurl.getRef(); //returns whatever after '#'
String[][] params = GetParameters(refrence);

and the defination for function GetParameters() is following

private String[][] GetParameters(String r)
{
   try
    {
       String[] p = r.split("&"); //separate parameters mixed with values
       String[][] data = new String[p.length][2];
       for(int i = 0 ; i<p.length; i++) //iterate whole array
        {
           data[i][0] = p[i].split("=")[0];  //parameter name
           data[i][1] = p[i].split("=")[1];  //parameter value
           data[i][1] = data[i][1].replace("%"," "); //replace % with space character
        }
        return data;  //return result
    }
    catch(Exception e)
    {
       return null;
    }
}

i have not executed and tested the code i am lazy one too so i hope you will accomodate lolz :D

Upvotes: 2

Related Questions