Search code examples
androiduriuribuilder

URI Builder - building a query string?


Im trying to build a String/URI to access an API. The API takes in a sentence param. Right now my attempt at doing this is the following:

//params[0] is a String such as "You will be a hero"
Uri.Builder builder = new Uri.Builder();
builder.scheme("https").authority("yoda.p.mashape.com/yoda").appendQueryParameter("sentence", params[0]);
Log.v("STRING TEST", builder.build().toString());

This is just my best attempt, and isn't working as I want. This currently outputs:

https://yoda.p.mashape.com%2Fyoda?sentence=you%20will%20be%20a%20hero

Im search for a way to build a URI that looks like the following:

https://yoda.p.mashape.com/yoda?sentence=you+will+be+a+hero

Any help would be appreciated. Thanks so much!


Solution

  • To fix your %2f look here: Uri Builder in android - '/' replaced by '%2F' and ":" is replaced by "%3A" and to fix the %20 try replacing the spaces with + before adding it to the builder.