UriMatcher will not match uri

I am trying to create my own search dialog. I use urimatcher to match uri. But that does not work. I always get the exception "java.lang.IllegalArgumentException: Unknown Uri: content: //com.simple.search.SuggestionProvider/search_suggest_query/? Limit = 50". Please explain this to me. What can I do to solve this problem?

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); } 

makeUrimatcher

 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" /> 

manifest

 .... .... <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> .... .... 
+4
source share
1 answer

To avoid discussion, I will modify this answer if you provide more information ...

but for now ...

you have android:searchSuggestIntentData="content://com.simple.search.SuggestionProvider/tests" in xml

so you need to change

 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; } 

If you do not see the differences, I added "tests /"

so now it will match content://com.simple.search.SuggestionProvider/tests/search_suggest_query?limit=50 , which will definitely send QSB ...

in any case, you can / must add a limit to your request

 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); 

and then in getSuggestions

 helper.getReadableDatabase().query(table, projection, selection, selectionArgs, null, null, sortOrder, limit); 

EDIT

AUTHORITY + "tests/" + SearchManager.SUGGEST_URI_PATH_QUERY should be the same as android:searchSuggestIntentData !!!

EDIT2: from the doc http://developer.android.com/guide/topics/search/adding-custom-suggestions.html

Choice of value in android: searchSuggestSelection attribute of your searchable file, or null if you have not declared android: searchSuggestSelection. More about using this to get the request below. selectionArgs Contains the search query as the first (and only) element of the array, if you have the android: searchSuggestSelection attribute declared in your search capability. If you have not declared android: searchSuggestSelection, then this parameter is zero. More about using this to get the request below.

add android:searchSuggestSelection=" ? "

+4
source

Source: https://habr.com/ru/post/1384812/


All Articles