ContentUris.withAppendedId 和 Uri.buildUpon().appendPath 之间的区别
Difference between ContentUris.withAppendedId and Uri.buildUpon().appendPath
我有点业余,正在学习 android 在线课程。我发现这两个不同的代码片段可以附加到 URI
public static Uri buildWeatherUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
这里我得到一个 URI,其中 id 附加到 CONTENT_URI
public static Uri buildWeatherLocation(String locationSetting) {
return CONTENT_URI.buildUpon().appendPath(locationSetting).build();
}
这里我得到一个 URI,其中 locationSetting 附加到 CONTENT_URI
我想知道两者是否具有相同的功能?
如果我们假设:
CONTENT_URI = content://com.example.myapp
然后
buildWeatherUri(5) -> content://com.example.myapp/5
buildWeatherLocation("location") -> content://com.example.myapp/location
现在让我们看看ContentUris'
source code:
public class ContentUris {
public static long parseId(Uri contentUri) {
String last = contentUri.getLastPathSegment();
return last == null ? -1 : Long.parseLong(last);
}
public static Uri.Builder appendId(Uri.Builder builder, long id) {
return builder.appendEncodedPath(String.valueOf(id));
}
public static Uri withAppendedId(Uri contentUri, long id) {
return appendId(contentUri.buildUpon(), id).build();
}
}
区别在于使用这两种方法:
appendEncodedPath
对比 appendPath
Encoding and Decoding URI Components
Each component of a URI permits a limited set of legal characters.
Other characters must first be encoded before they can be embedded in
a URI. To recover the original characters from a URI, they may be
decoded.
所以:
Appends the given segment to the path.
Encodes the given segment and appends it to the path.
appendPath (String newSegment) 对给定的路径段进行编码并将其附加到 Uri 对象的末尾。
withAppendedId (Uri contentUri, long id) 只是将给定的 Id 附加到给定的 Uri 对象的末尾,而不对其进行编码。
示例-
看看这个 URI http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22
因为它是一个 URL 我们必须使用 appendPath (String newSegment) 和 appendQueryParameter(String key, String value) 方法,因为它们都首先对传递给它们的参数进行编码,然后再附加它们。
Uri.parse("http://samples.openweathermap.org").buildUpon()
.appendPath("data")
.appendPath("2.5")
.appendPath("weather")
.appendQueryParameter("q", "London")
.appendQueryParameter("appid", "b6907d289e10d714a6e88b30761fae22")
.build();
查看此内容提供商的 URI content://com.example.app/ExampleTable/2
。
因为它在末尾包含一个数字,指向单个 table 项,因此要创建这样的 URI,我们必须使用 withAppendedId (Uri contentUri, long id) 使用 appendPath (String newSegment) 方法
添加路径段后的方法
Uri uri = Uri.parse("content://com.example.app").buildUpon()
.appendPath("ExampleTable")
.build()
Uri newUri = ContentUris.withAppendedId(uri, 2);
注意-
- Uri.Builder class 中定义的所有方法都对构建 Uri 对象有用。
- ContentUris class 中定义的所有方法都可用于处理使用
content
方案的 Uri 对象。
注-
路径段的编码应该在将它附加到 Uri 对象之前完成,特别是如果我们计划从它构建一个 URL 使用 -
URL url = new URL(Uri_to_string);
要在 Uri 对象的末尾附加字符串,称为 path,我们必须使用 appendPath (String newSegment)方法。
探索更多-
https://developer.android.com/reference/android/net/Uri.Builder.html
(Uri.Builder | 官方文档)
https://developer.android.com/reference/android/content/ContentUris.html
(ContentUris | 官方文档)
Why do you need to encode URLs?
(为什么需要编码 URLs?| Whosebug)
我有点业余,正在学习 android 在线课程。我发现这两个不同的代码片段可以附加到 URI
public static Uri buildWeatherUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
这里我得到一个 URI,其中 id 附加到 CONTENT_URI
public static Uri buildWeatherLocation(String locationSetting) {
return CONTENT_URI.buildUpon().appendPath(locationSetting).build();
}
这里我得到一个 URI,其中 locationSetting 附加到 CONTENT_URI
我想知道两者是否具有相同的功能?
如果我们假设:
CONTENT_URI = content://com.example.myapp
然后
buildWeatherUri(5) -> content://com.example.myapp/5
buildWeatherLocation("location") -> content://com.example.myapp/location
现在让我们看看ContentUris'
source code:
public class ContentUris {
public static long parseId(Uri contentUri) {
String last = contentUri.getLastPathSegment();
return last == null ? -1 : Long.parseLong(last);
}
public static Uri.Builder appendId(Uri.Builder builder, long id) {
return builder.appendEncodedPath(String.valueOf(id));
}
public static Uri withAppendedId(Uri contentUri, long id) {
return appendId(contentUri.buildUpon(), id).build();
}
}
区别在于使用这两种方法:
appendEncodedPath
对比 appendPath
Encoding and Decoding URI Components
Each component of a URI permits a limited set of legal characters. Other characters must first be encoded before they can be embedded in a URI. To recover the original characters from a URI, they may be decoded.
所以:
Appends the given segment to the path.
Encodes the given segment and appends it to the path.
appendPath (String newSegment) 对给定的路径段进行编码并将其附加到 Uri 对象的末尾。
withAppendedId (Uri contentUri, long id) 只是将给定的 Id 附加到给定的 Uri 对象的末尾,而不对其进行编码。
示例-
看看这个 URI
http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22
因为它是一个 URL 我们必须使用 appendPath (String newSegment) 和 appendQueryParameter(String key, String value) 方法,因为它们都首先对传递给它们的参数进行编码,然后再附加它们。
Uri.parse("http://samples.openweathermap.org").buildUpon() .appendPath("data") .appendPath("2.5") .appendPath("weather") .appendQueryParameter("q", "London") .appendQueryParameter("appid", "b6907d289e10d714a6e88b30761fae22") .build();
查看此内容提供商的 URI
content://com.example.app/ExampleTable/2
。因为它在末尾包含一个数字,指向单个 table 项,因此要创建这样的 URI,我们必须使用 withAppendedId (Uri contentUri, long id) 使用 appendPath (String newSegment) 方法
添加路径段后的方法Uri uri = Uri.parse("content://com.example.app").buildUpon() .appendPath("ExampleTable") .build()
Uri newUri = ContentUris.withAppendedId(uri, 2);
注意-
- Uri.Builder class 中定义的所有方法都对构建 Uri 对象有用。
- ContentUris class 中定义的所有方法都可用于处理使用
content
方案的 Uri 对象。
注-
路径段的编码应该在将它附加到 Uri 对象之前完成,特别是如果我们计划从它构建一个 URL 使用 -
URL url = new URL(Uri_to_string);
要在 Uri 对象的末尾附加字符串,称为 path,我们必须使用 appendPath (String newSegment)方法。
探索更多-
https://developer.android.com/reference/android/net/Uri.Builder.html (Uri.Builder | 官方文档)
https://developer.android.com/reference/android/content/ContentUris.html (ContentUris | 官方文档)
Why do you need to encode URLs? (为什么需要编码 URLs?| Whosebug)