【问题标题】:URI Matching on content provider not matching correctly内容提供者上的 URI 匹配不正确匹配
【发布时间】:2012-04-21 17:57:33
【问题描述】:

我已经尝试了一段时间,但我似乎无法解决我的 URI 匹配问题。非常感谢任何帮助。

这是我对内容 uri 和 uri 匹配器的声明:

private static final String AUTHORITY = "edu.uprm.civil.db";

public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);

private static UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
    sURIMatcher.addURI(AUTHORITY, "/get-table" + "/*", TABLE_REQUEST_CODE);
    sURIMatcher.addURI(AUTHORITY, "/get-row" + "/*", ROW_REQUEST_CODE);
    sURIMatcher.addURI(AUTHORITY, "/search" + "/*", TABLE_SEARCH_REQUEST_CODE);
}

而被传递的uri是用下面的代码构造的:

    Uri.Builder uriBuilder = DBContentProvider.CONTENT_URI.buildUpon();

    switch(id){
    case LOADER_GET_TABLE_ID :
        uriBuilder.appendPath("get-table");
        uriBuilder.appendPath("q");
        uriBuilder.appendQueryParameter("table", formID);
        return new CursorLoader(context, uriBuilder.build(), null, null, null, null);
    case ...

我调试了该方法,并且可以看到参数中包含的 URI,但它从不匹配。

uri 是怎么来的:

content://edu.uprm.civil.db/get-table/q?table=150

如果您需要更多信息,请告诉我,这让我很头疼......

【问题讨论】:

    标签: android pattern-matching uri android-contentprovider


    【解决方案1】:

    修改UriMatcher 的规则并从路径参数中删除起始/

    sURIMatcher.addURI(AUTHORITY, "get-table" + "/*", TABLE_REQUEST_CODE);
    sURIMatcher.addURI(AUTHORITY, "get-row" + "/*", ROW_REQUEST_CODE);
    sURIMatcher.addURI(AUTHORITY, "search" + "/*", TABLE_SEARCH_REQUEST_CODE);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-15
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2018-02-10
      相关资源
      最近更新 更多