Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Uri.Builder using "/" instead of "//" after scheme

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.

like image 632
OneCricketeer Avatar asked Dec 19 '22 18:12

OneCricketeer


1 Answers

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.

like image 148
Rod_Algonquin Avatar answered Dec 21 '22 09:12

Rod_Algonquin