📜  android studio 中的 Uri.builder - Java (1)

📅  最后修改于: 2023-12-03 14:39:08.593000             🧑  作者: Mango

Uri.Builder in Android Studio

Android Studio provides Uri.Builder class to create and manipulate URI (Uniform Resource Identifier) references. It is a convenient way to build a URI with the desired components, such as scheme, authority, path, query, and fragment. In this article, we will explore the Uri.Builder class and its methods in detail.

Creating Uri.Builder Object

We can create a new Uri.Builder object by calling the buildUpon() method of the Uri class. Here is an example:

Uri.Builder builder = Uri.parse("https://www.example.com").buildUpon();

In this code snippet, we create a new Uri.Builder object by calling the buildUpon() method on a Uri object that represents the base URL https://www.example.com. We can now use the methods of the Uri.Builder class to add or modify the components of the URI.

Adding Components to the Uri.Builder Object
Scheme

To add a scheme to the URI, we can use the scheme(String scheme) method. Here is an example:

builder.scheme("https");

In this code snippet, we set the scheme of the URI to https.

Authority

To add an authority to the URI, we can use the authority(String authority) method. Here is an example:

builder.authority("www.example.com");

In this code snippet, we set the authority of the URI to www.example.com.

Path

To add a path to the URI, we can use the path(String path) method. Here is an example:

builder.path("/index.html");

In this code snippet, we set the path of the URI to /index.html.

Query

To add a query to the URI, we can use the query(String query) method. Here is an example:

builder.query("param1=value1&param2=value2");

In this code snippet, we set the query of the URI to param1=value1&param2=value2.

Fragment

To add a fragment to the URI, we can use the fragment(String fragment) method. Here is an example:

builder.fragment("section1");

In this code snippet, we set the fragment of the URI to section1.

Building the Uri Object

Once we have added all the desired components to the Uri.Builder object, we can build the final Uri object by calling the build() method. Here is an example:

Uri uri = builder.build();

In this code snippet, we build the final Uri object.

Conclusion

In this article, we learned about the Uri.Builder class and its methods in Android Studio. We saw how to create a Uri.Builder object, add or modify its components, and build the final Uri object. With Uri.Builder, we can easily manipulate URIs in our Android applications.