0

I'm getting a data Uri out of my intent and try to parse the attributes. However, if the attribute contains a '+' sign, the getQueryParameter(attr) method will convert it to a ' ' as mentioned in the documentation

So this answer wont work if the parameter contains a '+' sign.

And this answer is converting the '+' to a '_'

My Uri looks somehow like this: "MyApp:///?Attr=fdwGcv+fsdsfd_AS="

Motassem Jalal
  • 1,254
  • 1
  • 22
  • 46

1 Answers1

0

Encode all the query parameter while creating the url.

String url = null;
    try {
        url = "MyApp:///?attr=" + URLEncoder.encode("fdwGcv+fsdsfd_AS=", "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    Uri uri = Uri.parse(url);

    System.out.print("attr::"+uri.getQueryParameter("attr"));
Praveen
  • 697
  • 6
  • 21
  • Have you tested this code? It is returning null. Even when calling getQueryParameterNames() it returns a Set with size 0\ – Motassem Jalal Jul 03 '17 at 10:03
  • The code is tested please check - I am using "uri.getQueryParameter("attr")" method – Praveen Jul 03 '17 at 10:05
  • Thank you for your answer, but I don't find it very convenient. How would the code change if I have more than one parameter separated with &. Why should I hard code the first parameter of the encode method? – Motassem Jalal Jul 03 '17 at 10:20
  • The answer is just to show how to encode the query parameter. you have to encode the query parameter before adding to url , if it has any special character check https://www.w3schools.com/tags/ref_urlencode.asp and https://stackoverflow.com/questions/1856785/characters-allowed-in-a-url. All the method like getQueryParameterNames() will still works and no need to hardcode anything. – Praveen Jul 03 '17 at 10:36