UriMatcher无法匹配Uri

4

我正在尝试为搜索对话框提供自定义建议。我正在使用urimatcher来匹配uri,但它不起作用。我始终会收到异常“java.lang.IllegalArgumentException:Unknown Uri:content://com.simple.search.SuggestionProvider/search_suggest_query/?limit=50”。请解释一下这是什么意思。我该如何解决这个问题?

private static final UriMatcher sURIMatcher = makeUriMatcher();
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    // Use UriMatcher, to find out what type of request received. Next, form
    // the corresponding query to the database
    switch (sURIMatcher.match(uri)) {
    case SEARCH_SUGGEST:
        if (selectionArgs == null) {
            throw new IllegalArgumentException(
                    "selectionArgs must be provided for the Uri: " + uri);
        }
        return getSuggestions(selectionArgs[0]);
    case SEARCH_TESTS:
        if (selectionArgs == null) {
            throw new IllegalArgumentException(
                    "selectionArgs must be provided for the Uri: " + uri);
        }
        return search(selectionArgs[0]);
    case GET_TEST:
        return getRecord(uri);
    default:
        throw new IllegalArgumentException("Unknown Uri: " + uri);
    }

创建Urimatcher
private static UriMatcher makeUriMatcher() {

    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    // For the record
    matcher.addURI(AUTHORITY, "tests", SEARCH_TESTS);
    matcher.addURI(AUTHORITY, "tests/#", GET_TEST);
    // For suggestions table
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY,
            SEARCH_SUGGEST);
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
            SEARCH_SUGGEST);
    return matcher;
}

logcat

11-30 14:16:27.295: I/ActivityThread(1638): Publishing provider com.simple.search.SuggestionProvider: com.simple.search.SuggestionProvider
11-30 14:16:35.424: D/com.simple.search.com.simple.search.SuggestionProvider(1638): Unknown Uri: content://com.simple.search.SuggestionProvider/search_suggest_query/?limit=50
11-30 14:16:35.424: E/DatabaseUtils(1638): Writing exception to parcel
11-30 14:16:35.424: E/DatabaseUtils(1638): java.lang.IllegalArgumentException: Unknown Uri: content://com.simple.search.SuggestionProvider/search_suggest_query/?limit=50
11-30 14:16:35.424: E/DatabaseUtils(1638):  at com.simple.search.SuggestionProvider.query(SuggestionProvider.java:122)
11-30 14:16:35.424: E/DatabaseUtils(1638):  at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:117)
11-30 14:16:35.424: E/DatabaseUtils(1638):  at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:98)
11-30 14:16:35.424: E/DatabaseUtils(1638):  at android.os.Binder.execTransact(Binder.java:287)
11-30 14:16:35.424: E/DatabaseUtils(1638):  at dalvik.system.NativeStart.run(Native Method)

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="@string/search_hint" 
    android:label="@string/app_label"
    android:searchSuggestAuthority="com.simple.search.SuggestionProvider"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestIntentData="content://com.simple.search.SuggestionProvider/tests"
    />

清单文件

....
....
        <activity
            android:label="@string/app_name"
            android:name=".SimpleSearch" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
                <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable"/>
        </activity>
....
....

1
URI 应该是 content://com.simple.search.SuggestionProvider/search_suggest_query?limit=50 而不是 content://com.simple.search.SuggestionProvider/search_suggest_query/?limit=50 - Selvin
@selvin 谢谢你,但还是不行。 - Binoy Babu
1
同样的错误?...你是如何执行这个查询的?从你的代码还是QSB(对于QSB,代码看起来不错)?添加L.d("TAG", uri.toString())...sURIMatcher是如何创建的?也许它不是这个匹配器,因为你是通过makeUriMatcher创建的...我习惯于将匹配器设置为final static,并在static{...}“构造函数”中构建。 - Selvin
@selvin 添加了urimatcher。仍然出现相同的错误。我从qsb查询。你能给一个最终静态匹配器的例子吗? - Binoy Babu
1
完整的logcat日志...+searchable.xml+从AndroidManifest.xml中定义的搜索活动...class provider { private final static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); static { matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST); } /*... rest of the provider class class */} - Selvin
@selvin 将这些添加到问题中 - Binoy Babu
1个回答

4
为了避免讨论,如果您提供更多信息,我会修改这个答案...
但是现在...
您的xml中有android:searchSuggestIntentData="content://com.simple.search.SuggestionProvider/tests" 因此您需要进行更改。
private static UriMatcher makeUriMatcher() {

    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    // For the record
    matcher.addURI(AUTHORITY, "tests", SEARCH_TESTS);
    matcher.addURI(AUTHORITY, "tests/#", GET_TEST);
    // For suggestions table
    matcher.addURI(AUTHORITY, "tests/" + SearchManager.SUGGEST_URI_PATH_QUERY,
            SEARCH_SUGGEST);
    matcher.addURI(AUTHORITY, "tests/" + SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
            SEARCH_SUGGEST);
    return matcher;
}

如果您没有看到我添加的"tests/",现在您可以看到了。
所以现在它将匹配content://com.simple.search.SuggestionProvider/tests/search_suggest_query?limit=50,这正是QSB将要发送的...
无论如何,您可以/应该向查询中添加限制。
case SEARCH_SUGGEST:
    if (selectionArgs == null) {
        throw new IllegalArgumentException(
                "selectionArgs must be provided for the Uri: " + uri);
    }
    final String limit = uri.getQueryParameter(SearchManager.SUGGEST_PARAMETER_LIMIT);
    return getSuggestions(selectionArgs[0], limit);

在getSuggestions中。
helper.getReadableDatabase().query(table, projection,
                    selection, selectionArgs, null, null, sortOrder, limit);

编辑:

AUTHORITY + "tests/" + SearchManager.SUGGEST_URI_PATH_QUERY应该与android:searchSuggestIntentData相同!!!

编辑2:来自文档http://developer.android.com/guide/topics/search/adding-custom-suggestions.html

选择在可搜索配置文件的android:searchSuggestSelection属性中提供的值,如果未声明android:searchSuggestSelection属性,则为null。有关使用此选项获取以下查询的更多信息。selectionArgs:如果在可搜索配置中声明了android:searchSuggestSelection属性,则包含搜索查询作为数组的第一个(且仅)元素。如果您未声明android:searchSuggestSelection,则该参数为null。有关使用此选项获取以下查询的更多信息。

添加android:searchSuggestSelection="?"


已按照您的建议添加了代码。但仍然抛出相同的异常。我跳过了limit,因为我还没有在getSuggestions()中实现它 :( - Binoy Babu
还有一个疑问,qsm返回的uri仍然是content://com.simple.search.SuggestionProvider/search_suggest_query吗?没有测试吗? - Binoy Babu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接