Skip to main content
Include pattern and additional options to showcase better how it can be used
Source Link
sunadorer
  • 3.9k
  • 35
  • 42

On Android you can use android.net.Uri. The following allows to create an Uri.Builder from an existing URL as String and then append:

Uri.parse(baseUrl) // Create Uri from String
    .buildUpon()   // Creates a "Builder"
    .appendEncodedPath("path/to/add")
    .appendQueryParameter("at_ref", "123") // To add ?at_ref=123
    .fragment("anker") // To add #anker
    .build()

Note that appendEncodedPath doesn't expect a leading / and only contains a check if the "baseUrl" ends with one, otherwise one is added before the path.

According to the docs, this supports

  • Absolute hierarchical URI reference following the pattern

    • <scheme>://<authority><absolute path>?<query>#<fragment>
  • Relative URI with pattern

    • <relative or absolute path>?<query>#<fragment>
    • //<authority><absolute path>?<query>#<fragment>
  • Opaque URI with pattern

    • <scheme>:<opaque part>#<fragment>

On Android you can use android.net.Uri. The following allows to create an Uri.Builder from an existing URL as String and then append:

Uri.parse(baseUrl) // Create Uri from String
    .buildUpon()   // Creates a "Builder"
    .appendEncodedPath("path/to/add")
    .build()

Note that appendEncodedPath doesn't expect a leading / and only contains a check if the "baseUrl" ends with one, otherwise one is added before the path.

On Android you can use android.net.Uri. The following allows to create an Uri.Builder from an existing URL as String and then append:

Uri.parse(baseUrl) // Create Uri from String
    .buildUpon()   // Creates a "Builder"
    .appendEncodedPath("path/to/add")
    .appendQueryParameter("at_ref", "123") // To add ?at_ref=123
    .fragment("anker") // To add #anker
    .build()

Note that appendEncodedPath doesn't expect a leading / and only contains a check if the "baseUrl" ends with one, otherwise one is added before the path.

According to the docs, this supports

  • Absolute hierarchical URI reference following the pattern

    • <scheme>://<authority><absolute path>?<query>#<fragment>
  • Relative URI with pattern

    • <relative or absolute path>?<query>#<fragment>
    • //<authority><absolute path>?<query>#<fragment>
  • Opaque URI with pattern

    • <scheme>:<opaque part>#<fragment>
Source Link
sunadorer
  • 3.9k
  • 35
  • 42

On Android you can use android.net.Uri. The following allows to create an Uri.Builder from an existing URL as String and then append:

Uri.parse(baseUrl) // Create Uri from String
    .buildUpon()   // Creates a "Builder"
    .appendEncodedPath("path/to/add")
    .build()

Note that appendEncodedPath doesn't expect a leading / and only contains a check if the "baseUrl" ends with one, otherwise one is added before the path.