6

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.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 2
    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 for more info][1] [1]: http://developer.android.com/guide/topics/providers/content-provider-creating.html#ContentURI – Rod_Algonquin Apr 07 '16 at 18:42
  • @Rod_Algonquin Ohh, I see that now in the other post :) Oops. Thank you. Feel free to answer. – OneCricketeer Apr 07 '16 at 18:44

1 Answers1

14

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.

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63