7

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.

2
  • 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]: developer.android.com/guide/topics/providers/… Commented Apr 7, 2016 at 18:42
  • @Rod_Algonquin Ohh, I see that now in the other post :) Oops. Thank you. Feel free to answer. Commented Apr 7, 2016 at 18:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I suppose I could have also read about URI's on Wikipedia and seen the authority follows the scheme. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.