我正在开发一个应用程序,它应该能够在多个设备之间同步数据存储。
我不明白为什么我无法从不是数据存储所有者的设备读取记录。而数据存储的所有者设备可以读取相同的记录。我必须准确地说,数据存储区是使用 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();
}
}
}
更新
我想对我的问题做出进一步的解释。我用一个实际案例来描述我的应用程序应该如何工作:
- 用户从他的设备打开应用
- 他连接到 dropbox API(他使用“APP_KEY 和 APP_SERVICE”连接),
- 他在 Dropbox 应用内创建了一个数据存储,
- 然后他创建记录(其中一个包含名为“titre”的字段),
B 用户从另一台设备打开应用,
- 他连接到同一个 dropbox API(他使用同一组“APP_KEY 和 APP_SERVICE”连接),
多亏了监听器,他的应用程序正在同步以在屏幕上显示由用户创建的新数据存储(我确信监听器能够完成其工作,因为我观看了通过 Toast 获取数据存储区的 ID [请参阅代码]),
- 问题:数据存储区永远不会显示,因为在监听器内部未找到名为“titre”的字段(请参阅代码内容)。
在 Debug模式下,我可以看到代码跳过这部分代码(见下文)。而其中有一条记录包含“titre”字段!
while (mRecord.hasNext()){
...
我希望我的详细信息会更清楚,请随时向我询问更多信息。如果您能帮助我,谢谢!
PS:还有一个细节,用户必须使用相同的邮件和密码连接到 Dropbox 才能共享其数据存储。否则他们无法共享他们的数据存储(尽管这是我所理解的)。我在这里做错了吗?
最佳答案
我没有完全遵循您在这里尝试做的事情,但有一些建议:
如果同一用户登录两台设备,则无需设置角色。您是每台设备的所有者。只需给网络时间来同步即可。
一般来说,另一台设备必须等待一定时间才能在您的数据存储列表中可见;然后你打开它,然后你必须再次等待,直到内容出现。
使用监听器是监视这些事件的好方法。
更新:我想你已经快到了。预计数据存储出现在列表中后,当您打开它时,它是空的。打开它的行为使图书馆开始在后台下载内容。下载完成后(有时仅部分完成),您的监听器将再次被调用。因此,如果您的监听器没有找到预期的“titre”记录,则应该放弃,稍后的调用将找到它。
更新:如果您希望不同的用户(使用不同的 Dropbox 帐户)共享数据存储,您必须设置公共(public)角色,但您还必须执行其他操作:所有者(数据存储的创建者)必须以某种方式将数据存储的数据存储 ID 传输给其他用户,并且(这是重要的部分!)他们的应用程序实例必须使用该 ID 调用 openDatastore()。在用户调用 openDatastore() 之前,该数据存储区不会显示在该用户的 listDatastores() 中。
祝你好运!
关于java - 是否可以从非所有者的 Android 设备上读取记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26691486/