1

I know how to deal with URLs such as: http://example.com/xyz. However, my URL contains question mark as follows: https://example.com/?xyz

So when I do the getIntent().getData().getPath() I get the output as /. The question marking is somehow limiting the processing. How do I deal with it?

Here is the intent-filter in my manifest:

<intent-filter>
    <action android:name="android.intent.action.VIEW"></action>
    <category android:name="android.intent.category.DEFAULT"></category>
    <category android:name="android.intent.category.BROWSABLE"></category>
    <data android:host="example.com" android:scheme="https"></data>
</intent-filter>

Here is the code from my Activity:

if (getIntent() != null && data != null && data.getPathSegments().size() >= 1) {
    Log.d("tagit", getIntent().getData().getPath().toString());
}

Now, the log shows blank.

Kevin Richards
  • 570
  • 1
  • 6
  • 23

2 Answers2

1

Well, I got it working by getting the complete link using the following:

data.getEncodedSchemeSpecificPart();

Thanks to this answer: https://stackoverflow.com/a/25295636/3739412

Community
  • 1
  • 1
Kevin Richards
  • 570
  • 1
  • 6
  • 23
0

You have to encode the URL to handle the question marks/white space/special characters. I am sharing a simple code snippet. It may help you.

String urlStr = "http://example.com/?xyz";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

And another more simpler solution can be:

private static final String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%";
String urlEncoded = Uri.encode(path, ALLOWED_URI_CHARS);
Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50
  • What do those parameters in URI() do? Also, I want the urlStr String to remain as such when sent to a user via an application such as SMS. Otherwise, it the user doesn't have my app installed, he/she won't be able to open the link via a browser. – Kevin Richards Jul 14 '15 at 05:17
  • I shared all of them as a generic solution. You may not need all those parameters. – Mohammad Arman Jul 14 '15 at 05:21
  • urlEncoded is still the same as path ("http://example.com/?xyz"). So my problem is not yet solved. – Kevin Richards Jul 14 '15 at 05:24