I was just wondering why it is advised to use a Uri.Builder
instead of just using just strings and concatenating the parameters to it?.
Can someone please explain it with some short example?
Is it because of efficiency (because Strings are immutable..etc (but then we can use Stringbuilder
or StringBuffer
) )
Or there are any security issues involved with Strings?
An URI
is quite complicated when you look at it from a distant perspective: there's a scheme, an authority, a path, queries... Moreover, you have to remember how to properly encode your data. All this is quite crumbersome and it is a repetitive task.
UriBuilder
is designed to abstract you away from all this. It will properly encode your parameters and will properly build your URI
without too much hassle and with a simple syntax:
UriBuilder.fromUri("http://localhost/").path("{a}").queryParam("name", "{value}").build("segment", "value");
will build the following URI: http://localhost/segment?name=value
. {a}
path will be replaced by the first parameter of build
and {value}
will be replaced by the second parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With