misaochan
misaochan

Reputation: 890

URI builder in Android, odd URL

I'm trying to use the URI builder in Android to create a URL with variables. I have read Use URI builder in Android or create URL with variables, but some symbols that I need aren't covered in that.

An example of URL that I want to create:

"https://commons.wikimedia.org/w/api.php?action=query&prop=categories|coordinates|pageprops&format=json&clshow=!hidden&coprop=type%7Cname%7Cdim%7Ccountry%7Cregion%7Cglobe&codistancefrompoint=38.11386944444445%7C13.356263888888888&generator=geosearch&redirects=&ggscoord=38.11386944444445%7C13.356263888888888&ggsradius=100&ggslimit=10&ggsnamespace=6&ggsprop=type%7Cname%7Cdim%7Ccountry%7Cregion%7Cglobe&ggsprimary=all&formatversion=2"

I will want to substitute the ggscoord with a variable. I know how to append the path and query parameters... but what builder method should I use for the %7C and | ?

Upvotes: 2

Views: 1539

Answers (2)

starkshang
starkshang

Reputation: 8528

Character like %7C has been encoded,maybe it's a Chinese code or others,so if you want to add some parameters to your URL,just use URLEncoder.encode to encode your primitive string. like this:

Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
.authority("www.lapi.transitchicago.com")
.appendPath("api")
.appendPath("1.0")
.appendPath("ttarrivals.aspx")
.appendQueryParameter("key", "[redacted]")
.appendQueryParameter("mapid",URLEncoder.encode("&&&","UTF-8"); //note this

Upvotes: 5

Ugur Ozmen
Ugur Ozmen

Reputation: 580

In order to modify query, you may use appendQueryParameter for plain strings like "|", any you may use encodedQuery for pre-encoded strings like "%7C".

There is no append method for pre-encoded query parameters.

Upvotes: 5

Related Questions