【问题标题】:new to android - understanding source of content providerandroid新手——了解内容提供者的来源
【发布时间】:2014-01-07 18:27:20
【问题描述】:

在处理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 文档吗?这些数据的格式到底是什么...我如何查看它?

【问题讨论】:

  • 确保 url 正确,因为它没有在我的浏览器中打开。我觉得你很困惑。
  • 您在上面使用的 URL 是用于内容提供者 (Android LOCAL) 的,不能在万维网上使用。如果您尝试从服务器加载/拉取代码,那么您需要一个网络解决方案。 ContentProviders 不是这样的。
  • 我认为这个问题的 URI 和 URL 有混淆

标签: java android android-contentprovider


【解决方案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 通常由希望管理数据库并将该信息提供给其他应用程序同时最小化访问冲突等的应用程序使用。

编辑:
此代码来自您的教程。查看添加的 cmets:

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

您在 cmets 中的问题是:

“我只需要这个文件的一个示例:String URL = "content://com.example.provider.College/students";,数据会是什么样子?”

这个问题的答案是,您的手机上某处有一个 Sqlite 数据库 - 通常(在这种情况下肯定是)由您正在访问的应用程序和/或内容提供商创建。您还知道内容解析器接受此 URI 和一些其他信息,并将返回一个 CURSOR。

这个问题解决了什么是光标。

use of cursor in android

如果你完整阅读教程,你会发现这段代码::

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 的注册是什么。

您会注意到您的 URL 与“PROVIDER_NAME”和“URL”有着惊人的相似之处。这是因为 ContentProvider 正在利用这些值来将自己标识为该部分 URI 到 android 系统的解析器。

您应该按照教程中的说明创建文件,制作示例应用程序功能,您将能够开始更清楚地理解这一点。

【讨论】:

  • 我怎样才能创建自己的 url 以便我可以做这种教程?我可以将它上传到我的服务器。我只需要查看这种url的数据格式
  • 您是否正在尝试向/从 Web 服务器加载数据?如果是这样,那么 ContentProviders 不是您要找的。您需要一个网络解决方案。请参阅:developer.android.com/guide/topics/providers/… 如果您想在本地创建数据库。
  • 我只需要这个文件的一个例子: String URL = "content://com.example.provider.College/students"; ,数据会是什么样子?
【解决方案2】:

这不是真的,也不是网址。这是一个假设的ContentURI 示例。

例如,您可以像这样咨询UserDictionary -

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

你也可以create你自己的。

【讨论】:

  • 或者,更好的是,我如何创建自己的 contentURI?我有一个可以上传到的服务器。
  • 他们住在电话里,我在上次编辑中添加了link。
  • 如果我理解正确的话,content://com.example.provider.College/students 是手机上的某种数据文件吗? (我以为是互联网上的某种网站或xml文件)
  • 是的。这是手机上的“假设”(com.example 表示它是一个example)数据库。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多