The Uri.Builder
class in Android is a utility class that can be used to construct Uri
objects in a safe and efficient manner. It provides a convenient way to build Uri
objects that adhere to the rules of the URI syntax, without the need to manually construct the Uri
string.
Here is an example of how to use the Uri.Builder
class to construct a Uri
object in Android:
Uri.Builder builder = new Uri.Builder(); builder.scheme("https") .authority("www.example.com") .path("api") .appendPath("users") .appendPath("123") .appendQueryParameter("format", "json"); Uri uri = builder.build();
This code creates a Uri.Builder
object and adds the various components of the Uri
(scheme, authority, path, etc.) using the various scheme
, authority
, path
, and appendPath
methods. It also adds a query parameter using the appendQueryParameter
method. Finally, it calls the build
method to construct the Uri
object.
You can then use the Uri
object in various ways, such as using it to send a request to a web server or to open a webpage in a browser.
Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);