Uri.builder vs string based url construction

1.6k Views Asked by At

Why would one use Uri.Builder when Urls can be constructed by simply concatenating string variables?

In which cases is advisable to use uribuilder over string variables and vice-versa?

3

There are 3 best solutions below

2
On

Uri.Builder is a helper class using which you can offload the work of constructing various kinds of uri. It is always recommended to use this class instead of creating it yourself.

Uri.Builder

0
On

Only to make simple and easy to build URIs safely.

0
On

Always prefer uribuilder over strings to build a uri as uribuilder has a lot of built-in validation to ensure that the given address is well-formed. Generally speaking, if you know that your uri is going to remain constant through out then you can go ahead and use a string but in the case when you will be often re-constructing your uri by changing/replacing fields, uribuilder is a must!