0

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!

Brent Aureli
  • 463
  • 6
  • 21

1 Answers1

1

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.

Community
  • 1
  • 1
  • Fixed the %2F by using appendPath. However replacing the spaces with + resulted in the a similar problem, the appendQueryParameter converted all the + into %2B instead of keeping them. – Brent Aureli Oct 13 '14 at 19:40
  • I fixed this using .encodedQuery("sentence=" + params[0]) instead of appendQueryParameter – Brent Aureli Oct 13 '14 at 19:55