2014-11-01 22 views
0

我正在開發一個應用程序,它應該能夠在幾個設備之間同步數據存儲。是否有可能從不是它的擁有者的Android設備讀取記錄?

我想不通爲什麼我不能從不是數據存儲所有者的設備讀取記錄。而數據存儲的所有者設備可以讀取相同的記錄。我必須確定數據存儲已經使用EDITOR權限創建(見下文)。

try { 
    datastoreTitle = mDatastoreManager.createDatastore(); 
    datastoreTitle.setRole(DbxPrincipal.PUBLIC, DbxDatastore.Role.EDITOR); 
    } catch (DbxException e) { 
     e.printStackTrace(); 
    } 

有人遇到同樣的問題嗎?提出這個問題的代碼如下:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 



    /** 
    * Opening of the database 
    */ 
    db = new SQliteHelper(this) ; 



    /** 
    * Connection to dropbox API 
    */ 
    mAccountManager = DbxAccountManager.getInstance(getApplicationContext(), APP_KEY, APP_SECRET); 


    // Set up the datastore manager 
    if (mAccountManager.hasLinkedAccount()) { 
     try { 
      // Use Dropbox datastores 
      mDatastoreManager = DbxDatastoreManager.forAccount(mAccountManager.getLinkedAccount()); 

     } catch (DbxException.Unauthorized e) { 
      System.out.println("Account was unlinked remotely"); 
     } 


    } 
    if (mDatastoreManager == null) { 
     // Account isn't linked yet, use local datastores 
     mDatastoreManager = DbxDatastoreManager.localManager(mAccountManager);    

     AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); 
     alert.setTitle("Connection à Dropbox"); 
     alert.setMessage("L'initialisation de l'application sert à vous synchroniser avec l'espace de données partagées.\r\n" 
       + "Souhaitez-vous synchroniser votre application?"); 

     alert.setPositiveButton("Oui", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 

      mAccountManager.startLink((Activity)MainActivity.this, REQUEST_LINK_TO_DBX); 

      } 
     }); 

     alert.setNegativeButton("Non", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

       Intent returnIntent = new Intent(); 
       setResult(RESULT_CANCELED, returnIntent); 
       finish(); 


      } 
     }); 


     alert.show(); 
    } 

/** 
* Listener 
*/ 

mDatastoreManager.addListListener(new DbxDatastoreManager.ListListener() { 
    @Override 
    public void onDatastoreListChange(DbxDatastoreManager dbxDatastoreManager) { 

List<TitleList> listInBDD = db.getTitleSQliteHelper(); 
Set<DbxDatastoreInfo> datastorePresent = null; 
try { 
    datastorePresent = mDatastoreManager.listDatastores(); 
} catch (DbxException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 
Iterator<DbxDatastoreInfo> mdbxDatastoreInfoIterator = datastorePresent.iterator() ; 

while (mdbxDatastoreInfoIterator.hasNext()){ 

    DbxDatastoreInfo mdbxDatastoreInfo = mdbxDatastoreInfoIterator.next(); 
    boolean find = false ; 
    Log.d("Test 1 - delete Datastore => mdbxDatastoreInfo.id :" , mdbxDatastoreInfo.id) ; 

    for (TitleList titleFound : listInBDD){ 
     if (titleFound.idDbx.equals(mdbxDatastoreInfo.id)){ 
      Log.d("Test 2 - delete Datastore => TitleList :" , titleFound.nom.toString()) ; 

      find = true ; 
     } 
    } 

    /** 
    * Problem comes below 
    */ 


    if (!find){ 
     Log.d("Delete absent datastore inside of the BDD : " , mdbxDatastoreInfo.id) ; 
     try { 

      DbxDatastore dbxStore = mDatastoreManager.openDatastore(mdbxDatastoreInfo.id); 
      // Toast to check that the datastores have been found 
      Toast.makeText(MainActivity.this, dbxStore.getId(), Toast.LENGTH_LONG).show(); 
      DbxTable tab = dbxStore.getTable("table_de_courses"); // All is fine up to here, "tab" seems empty for a device which is not the owner of the database 
      QueryResult mResults = tab.query(); 
      Iterator<DbxRecord> mRecord = mResults.iterator(); 

      // When the device is not the owner of the database, the code stops at this while condition 
      while (mRecord.hasNext()){ 

       DbxRecord tmpRecord = mRecord.next(); 
       Set<String> fieldList = tmpRecord.fieldNames(); 
       Iterator<String> fieldNameList = fieldList.iterator(); 

       while (fieldNameList.hasNext()){ 

        String str = fieldNameList.next(); 

        if (str.equalsIgnoreCase("titre")){ 
          TitleList tit = new TitleList(tab.get("titre").getString("titre")); 
          tit.setConnectDropbox(); 
          tit.setIdDbx(mdbxDatastoreInfo.id); 
          db.addLists(tit); 
         } 

       } 

      } 


     } catch (DbxException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

UPDATE

我想帶來進一步的解釋我的問題。我坐之實踐情況來形容我的應用程序應該如何工作:

  • 用戶打開他的設備應用
    • 他連接到Dropbox的API(他聯繫,利用「APP_KEY和APP_SERVICE」) ,
    • 他創建Dropbox的應用程序的內部數據存儲,
    • 然後他創造記錄(含稱爲「滴度」包含場其中之一),
  • B的用戶打開來自其他設備的應用程序,

    • 他連接到相同的Dropbox API(他與同一對夫婦的 「APP_KEY和APP_SERVICE」 連接),
    • 得益於聽衆,他的應用程序正在同步屏幕上顯示由用戶創建的新數據存儲(我確信由於我通過Toast觀看數據存儲的ID,因此我傾聽了聽衆的工作,請參閱代碼],

      • 問題:數據存儲將永遠不會顯示,因爲名爲「titre」的字段在偵聽器內部找不到(請參閱代碼內容)。

在調試模式下我可以看到代碼跳過這部分代碼(見下文)。而有一個包含「titre」字段的記錄!

while (mRecord.hasNext()){ 
... 

我希望我的細節會更清楚,不要猶豫,向我提供進一步的信息。謝謝,如果你能幫助我!

PS:一個細節,用戶必須連接與相同的郵件和密碼到收存箱分享他們的數據存儲。否則,他們不能共享他們的數據存儲(儘管如此,這是我所理解的)。我在這裏犯了一個錯誤嗎?

+0

作爲對P.S .: No的迴應,用戶不需要(也不應該)共享用戶名和密碼以共享數據存儲區。這行:datastoreTitle.setRole(DbxPrincipal.PUBLIC,DbxDatastore.Role.EDITOR);在該數據存儲上設置權限,以便具有該數據存儲的數據存儲ID的任何Dropbox用戶都可以對其進行編輯。他們可以並且應該使用他們自己的賬戶登錄他們自己的用戶名和密碼。 – Greg 2014-11-03 17:53:33

回答

5

我不完全跟隨你想在這裏做什麼,但一些建議:

  • 如果同一用戶在兩臺設備登錄,您不需要設置角色。您是每臺設備上的所有者。只需給網絡時間進行同步。

  • 通常,其他設備必須等待一段時間才能在您的數據存儲列表中可見;然後你打開它,然後你必須等待,直到內容出現。

使用監聽器是觀察這些事件的好方法。

更新:我覺得你快到了。預計在數據存儲出現在列表中後,當您打開它時,它就是空的。打開它的行爲使圖書館開始下載後臺內容。當下載完成時(有時只是部分完成),您的監聽器將再次被調用。所以你的聽衆應該放棄,如果它沒有找到預期的「滴定」記錄,稍後的電話會找到它。

更新過多:如果您希望不同的用戶(使用不同的Dropbox帳戶)共享數據存儲,你必須設置的公共角色,但你必須做其他事情:對所有者(創建者)數據存儲必須以某種方式將數據存儲的數據存儲ID傳送給其他用戶,並且(這是重要的一部分!)他們的應用程序實例必須使用該ID調用openDatastore()。在用戶調用openDatastore()之前,數據存儲不會顯示在該用戶的listDatastores()中。

祝你好運!

+0

對不起,如果我不清楚。我想要的是讓不同的用戶連接到在Dropbox開發人員平臺上創建的一個應用程序(在「應用程序控制臺」選項卡下)。我要更新我的問題來澄清我的問題(我也更新了代碼,因爲實際上這個代碼是由一個監聽器調用的)。謝謝你的幫助! – KevHV 2014-11-03 07:18:40

相關問題