我正在開發一個應用程序,它應該能夠在幾個設備之間同步數據存儲。是否有可能從不是它的擁有者的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:一個細節,用戶必須連接與相同的郵件和密碼到收存箱分享他們的數據存儲。否則,他們不能共享他們的數據存儲(儘管如此,這是我所理解的)。我在這裏犯了一個錯誤嗎?
作爲對P.S .: No的迴應,用戶不需要(也不應該)共享用戶名和密碼以共享數據存儲區。這行:datastoreTitle.setRole(DbxPrincipal.PUBLIC,DbxDatastore.Role.EDITOR);在該數據存儲上設置權限,以便具有該數據存儲的數據存儲ID的任何Dropbox用戶都可以對其進行編輯。他們可以並且應該使用他們自己的賬戶登錄他們自己的用戶名和密碼。 – Greg 2014-11-03 17:53:33