Kyryl Zotov
Kyryl Zotov

Reputation: 1968

Why use Uri.Builder

I am using StringBuilder to create URI to iterate with JSON Api.

Some people recommend to use Uri.Builder for that purpose but can't explain why. Isn't that even redundant?

Upvotes: 2

Views: 345

Answers (1)

Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23962

Assuming that you're talking about android.net.Uri (and not java.net.URI), I see 3 reasons:

  1. It does URL encoding for you, in case if your HTTP-client/library/whatever doesn't
  2. It has convenient methods for adding query params (such as appendQueryParameter)
  3. It does some validation (but, as documentation says, you should not rely on that)

I would say it is not something you would start holy-war about, but I prefer not using Uri as this class is part of Android SDK - this makes your component automatically dependent on Android OS (therefore it's a bit harder to test and maintain).

Upvotes: 3

Related Questions