📅  最后修改于: 2023-12-03 14:39:08.593000             🧑  作者: Mango
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.
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.
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
.
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
.
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
.
To add a query to the URI, we can use the query(String query)
method. Here is an example:
builder.query("param1=value1¶m2=value2");
In this code snippet, we set the query of the URI to param1=value1¶m2=value2
.
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
.
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.
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.