Android ContentProvider URI 格式用于不在_id 上的选择查询

标签 android rest android-contentprovider

我正在尝试理解 Android REST 风格的 ContentProvider URI。假设我有一个消息数据库,每个消息都有一个“_id”和“收件人”字段(以及其他字段)。

我了解如何获取所有消息的所有列:

// Get all messages.
URI uri = Uri.parse("content://" + AUTHORITY + "/messages");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);

我了解如何获取_id 为 1 的消息:

// Get the message with _id 1.
URI uri = Uri.parse("content://" + AUTHORITY + "/messages/1");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);

如果我想获取收件人值为 foo@bar.com 的邮件怎么办?

// Get the messages with recipient "foo@bar.com"
URI uri = Uri.parse("content://" + AUTHORITY + ??);  // What goes here?
Cursor cursor = getContentResolver().query(uri, null, 
    "recipient = ?", "foo@bar.com", null);

如果这样更好的话,我很乐意在 URI 中传递收件人值:

// Get the messages with recipient "foo@bar.com"/
URI uri = Uri.parse("content://" + AUTHORITY + ?? + "foo@bar.com");  // What goes here?
Cursor cursor = getContentResolver().query(uri, null, null, null, null);

对于最后一个用例,首选的 URI 格式是什么?

更新:我是 ContentProvider 和客户端的作者,所以我可以使用任何我想要的格式。我仍然想知道什么是最佳实践

最佳答案

In this last case, what should go after the AUTHORITY in the URI?

随心所欲。如果您正在编写 ContentProvider,则由您来定义您的 Uri 语法的含义,对于权限右侧的路径段。

在您的例子中,您正在通过查询参数传递值 (foo@bar.com)。因此,您的 ?? 可以是:

  • 什么都没有
  • 一些路径段可以帮助您识别您正在查询的表类型,例如 messages
  • asrasiojdflkasdjkl90238490qa89dsfjlas 或其他一些随机字符串
  • 等等

关于Android ContentProvider URI 格式用于不在_id 上的选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23188307/

相关文章:

wcf - 从命令提示符调用休息服务

java - 针对来自不同领域但具有共同关系的实体的 REST API 设计

android - 我的 Android ContentProvider 找不到 ContentResolver

android - SocketTimeoutException 安卓

android - 亚行将不会运行

rest - 单个服务 AngularJS 的多个路由 URL

android - 将参数传递给自定义搜索建议 ContentProvider

Android 何时使用 ContentResolver applyBatch 或 BulkInsert

android - 单击 MultiAutoCompleteTextView 时不显示键盘

android - 为什么我得到 java.io.IOException : Mark has been invalidated?