I dug through the source code, but I just can't find a reason for this behavior.
According to Use URI builder in Android or create URL with variables, this should work absolutely fine.
Say I want to connect to https://www.gravatar.com/eac16c9fc481cb6825f8b3a35f916b5c.json
Then, I have this code to print three separate ways of getting that address.
String userHash = "eac16c9fc481cb6825f8b3a35f916b5c";
String correctUrl = "https://www.gravatar.com/" + userHash + ".json";
Uri.Builder builder1 = Uri.parse(correctUrl).buildUpon();
Uri.Builder builder2 = new Uri.Builder()
.scheme("https")
.path("www.gravatar.com")
.appendPath(userHash + ".json");
Log.i("Correct URL", correctUrl);
Log.i("Builder 1 URL", builder1.toString());
Log.i("Builder 2 URL", builder2.toString());
The first two print fine, but that third one is what I would prefer to use, but it isn't correct as you can see http:/www
instead of http://www
I/Correct URL: https://www.gravatar.com/eac16c9fc481cb6825f8b3a35f916b5c.json
I/Builder 1 URL: https://www.gravatar.com/eac16c9fc481cb6825f8b3a35f916b5c.json
I/Builder 2 URL: https:/www.gravatar.com/eac16c9fc481cb6825f8b3a35f916b5c.json
I am compiling with API 23, and I haven't bothered to try a different API version because I am just confused why this wouldn't work.
You need to set the URL path to the authority()
, instead to the path
which will always give you 1 forward slash. remove .path("www.gravatar.com")
add authority("www.gravatar.com")
,
Here is more info why authority is used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With