问题
I've been trying now for a while but I can't seem to resolve my URI matching problem. Any help is greatly appreciated.
Here is my declaration of the content uri and the uri matcher:
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);
}
And the uri being passed is contructed in the following code:
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 ...
I debug the method and I am able to see the URI as it comes in the parameter, but it never matches.
How the uri comes:
content://edu.uprm.civil.db/get-table/q?table=150
If you need any more info please let me know, it's been a headache for me...
回答1:
Modify the rules for your UriMatcher and remove the starting / from the path parameter:
sURIMatcher.addURI(AUTHORITY, "get-table" + "/*", TABLE_REQUEST_CODE);
sURIMatcher.addURI(AUTHORITY, "get-row" + "/*", ROW_REQUEST_CODE);
sURIMatcher.addURI(AUTHORITY, "search" + "/*", TABLE_SEARCH_REQUEST_CODE);
来源:https://stackoverflow.com/questions/10256773/uri-matching-on-content-provider-not-matching-correctly