当我处理following tutorial时,我遇到了这段代码:
public void onClickRetrieveStudents(View view) {
// Retrieve student records
String URL = "content://com.example.provider.College/students";
我很想知道这是什么类型的数据,所以我试图访问网站http://com.example.provider.College/students来查看数据,但它只是给出了某种错误。因此我的问题是,这个URL是某种xml文件吗?这个数据的格式究竟是什么......我该如何查看?
答案 0 :(得分:1)
我建议您熟悉以下文档:
内容提供商:
http://developer.android.com/guide/topics/providers/content-providers.html
基本上当你将那个“URL”传递给ContentResolver时(可能你正在做这样的事情):
// Queries the user dictionary and returns results
mCursor = getContentResolver().query(
UserDictionary.Words.CONTENT_URI, // The content URI of the words table
mProjection, // The columns to return for each row
mSelectionClause // Selection criteria
mSelectionArgs, // Selection criteria
mSortOrder); // The sort order for the returned rows
您要求android将该URL解析为设置为处理该URL的ContentProvider。 URL不是“虚构的”,因为它的目标是本地对象和进程,它们存在并由使用ContentProvider机制存储和向其他应用程序提供数据的应用程序定义。
该URL的目标(在这种情况下转换为URI)是指定您想要的ContentProvider以及您想要的内容。
ContentProviders通常由希望管理数据库并将该信息提供给其他应用程序的应用程序使用,同时最大限度地减少访问冲突等。
编辑:
此代码来自您的教程。请参阅添加的评论:
/// this url points to the content provider.
//The content provider uses it to
///reference a specific database which it has knowledge of
//This URI doesn't represent an
//actual FILE on your system, rather it represents a way for you to tell the content //provider what DATABASE to access and what you want from it.
String URL = "content://com.example.provider.College/students";
// This line converts yoru "URL" into a URI
Uri students = Uri.parse(URL);
/// This call returns a Cursor - a cursor is a object type which contains the results of your QUERY in an order manner. IN this case it is a set of rows, each of which has a number of columns coresponding to your query and database, which can be iterated over to pull information from the DB..
/// managedQuery takes, as an argument, the URI conversion of the URL - this is
// where you are actually calling to the contentprovider, asking it to do a query on the
// databse for some information
Cursor c = managedQuery(students, null, null, null, "name");
// This line moves to the first ROW in the cursor
if (c.moveToFirst()) {
// this does somethign as long as the while loop conditional is true.
do{
// This line creates a pop up toast message with the information stored in the columns of the row you the cursor is currently on.
Toast.makeText(this,
c.getString(c.getColumnIndex(StudentsProvider._ID)) +
", " + c.getString(c.getColumnIndex( StudentsProvider.NAME)) +
", " + c.getString(c.getColumnIndex( StudentsProvider.GRADE)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
评论中的问题是:
“我只需要这个文件的一个例子:String URL =”content://com.example.provider.College/students“;,数据会是什么样子?”
答案是你手机上有一个Sqlite数据库 - 一般(在这种情况下肯定)是由你正在访问的应用程序和/或内容提供商创建的。您还知道内容解析器接受此URI和其他一些信息,并将返回CURSOR。
这个问题解决了光标的问题。
如果您完全阅读本教程,您将找到此代码::
public class StudentsProvider extends ContentProvider {
static final String PROVIDER_NAME = "com.example.provider.College";
static final String URL = "content://" + PROVIDER_NAME + "/students";
static final Uri CONTENT_URI = Uri.parse(URL);
static final String _ID = "_id";
static final String NAME = "name";
static final String GRADE = "grade";
您还可以在教程的清单中找到:
<provider android:name="StudentsProvider"
android:authorities="com.example.provider.College">
</provider>
您的ContentProvider会针对相关URI注册。
您会注意到您的网址以及“PROVIDER_NAME”和“网址”具有怪异的相似之处。这是因为ContentProvider正在利用这些值来识别自己作为Android系统的这个部分URI的解析器。
您应该按照教程中的描述创建文件,制作示例应用程序功能,然后您就可以更清楚地了解这一点。
答案 1 :(得分:0)
这不是真的,也不是网址。这是一个假设ContentURI的例子。
例如,您可以像{ - 3}}一样咨询 -
// Queries the user dictionary and returns results
mCursor = getContentResolver().query(
UserDictionary.Words.CONTENT_URI, // The content URI of the words table
mProjection, // The columns to return for each row
mSelectionClause // Selection criteria
mSelectionArgs, // Selection criteria
mSortOrder); // The sort order for the returned rows
你也可以UserDictionary自己的。