user3847870
user3847870

Reputation: 410

Why use Uri.Builder and not just string and concatente parameters to it?

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?

Upvotes: 8

Views: 5479

Answers (1)

Tunaki
Tunaki

Reputation: 137104

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.

Upvotes: 7

Related Questions