2014-01-07 23 views
0

當我通過following tutorial工作,我遇到了這個代碼:新的Android - 內容提供商的認識來源

​​

我很感興趣,看看這是什麼樣的數據,所以我試圖去到網站http://com.example.provider.College/students來查看數據,但它只是給了一些錯誤。所以我的問題是,這個URL是某種xml文件嗎?這個數據的格式究竟是什麼......我如何查看它?

+0

確定該網址是正確的,因爲它不會在我的瀏覽器中打開。我覺得你很困惑。 – Raghunandan

+0

您上面使用的URL是針對內容提供者(Android LOCAL)的,不適用於WorldWide Web。如果您試圖從服務器加載/提取代碼,則需要聯網解決方案。 ContentProviders是不是。 –

+0

我認爲這個問題有一個URI和URL的混合 – Magnus

回答

0

這不是真的,它不是一個網址。這是假設的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自己。

+0

你能告訴我一個真正的ContentURI – user3166666

+0

的例子,或者更好的是,我如何創建自己的contentURI?我有一個服務器,我可以上傳到。 – user3166666

+0

他們生活在電話中,我在最後一次編輯中添加了一個[鏈接](http://developer.android.com/guide/topics/providers/content-provider-creating.html)。 –

1

我建議你熟悉以下documenation:

內容提供商:

http://developer.android.com/guide/topics/providers/content-providers.html

本質上講,當你傳遞 「URL」 的ContentResolver的(想必你正在做somethign像這個):

// 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到ContentProvider whic h設置爲處理該URL。 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()); 
      } 

您的評論的問題是:

「我需要的是這個文件的一個例子:字符串URL =」內容://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。

您會注意到您的網址和「PROVIDER_NAME」和「網址」具有令人驚異的相似之處。這是因爲ContentProvider正在利用這些值來將自己標識爲這個partiuclar URI到android系統的解析器。

您應該按照教程中所述創建文件,製作示例應用程序功能,並且您將能夠更清楚地理解這一點。

+0

我如何創建我自己的網址,以便我可以做這種教程?我可以將其上傳到我的服務器。我只需要查看這種URL的數據格式 – user3166666

+0

您是否試圖從/從Web服務器加載數據?如果是這樣,那麼ContentProviders不是你要找的。你需要一個網絡解決方案。 請參閱:http://developer.android.com/guide/topics/providers/content-provider-creating.html如果您打算在本地創建Databaase。 –

+0

所有我需要的是這個文件的一個例子:String URL =「content://com.example.provider.College/students」; ,數據會是什麼樣子? – user3166666