Java 是否有可能从一个不是其所有者的Android设备读取记录?
我正在开发一个应用程序,它应该能够在多个设备之间同步数据存储 我不明白为什么我不能从不是数据存储所有者的设备读取记录。而数据存储的所有者设备可以读取相同的记录。我必须确定数据存储是使用编辑器权限创建的(见下文) 有人也遇到过同样的问题吗?提出此问题的代码如下:Java 是否有可能从一个不是其所有者的Android设备读取记录?,java,android,dropbox-api,Java,Android,Dropbox Api,我正在开发一个应用程序,它应该能够在多个设备之间同步数据存储 我不明白为什么我不能从不是数据存储所有者的设备读取记录。而数据存储的所有者设备可以读取相同的记录。我必须确定数据存储是使用编辑器权限创建的(见下文) 有人也遇到过同样的问题吗?提出此问题的代码如下: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.m
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();
}
}
}
我希望我的细节会更清楚,不要犹豫,问我进一步的信息。如果你能帮助我,谢谢你
PS:还有一个细节,用户必须使用相同的邮件和密码连接到dropbox才能共享他们的数据存储。否则他们就不能共享他们的数据存储(尽管这是我所理解的)。我在这里做错了吗?我并没有完全按照您在这里试图做的事情来做,但有一些建议:
- 如果同一用户同时登录到两个设备,则无需设置角色。您是每个设备的所有者。只需给网络时间进行同步
- 一般来说,另一个设备必须等待一定时间才能在您的数据存储列表中显示;然后你打开它,然后你必须再次等待,直到内容出现
祝你好运 我并没有完全遵循您在这里试图做的事情,但有一些建议:
- 如果同一用户同时登录到两个设备,则无需设置角色。您是每个设备的所有者。只需给网络时间进行同步
- 一般来说,另一个设备必须等待一定时间才能在您的数据存储列表中显示;然后你打开它,然后你必须再次等待,直到内容出现
祝你好运 对不起,我不清楚。我想要的是让不同的用户连接到dropbox开发者平台上创建的一个应用程序(在“应用程序控制台”选项卡下)。我将更新我的问题以澄清我的问题(我也会更新代码,因为实际上这个代码是由侦听器调用的)。谢谢你的帮助!对不起,我不清楚。我想要的是让不同的用户连接到dropbox开发者平台上创建的一个应用程序(在“应用程序控制台”选项卡下)。我将更新我的问题以澄清我的问题(我也会更新代码,因为实际上这个代码是由侦听器调用的)。谢谢你的帮助!回答:不,用户不需要(也不应该)共享用户名和密码来共享数据存储。此行:datastoreTitle.setRole(DbxPrincipal.PUBLIC、DbxDatastore.Role.EDITOR);设置该数据存储上的权限,以便具有该数据存储的数据存储ID的任何Dropbox用户都可以对其进行编辑。他们可以也应该使用自己的帐户,并使用自己的用户名和密码登录。回答问题:否,用户不需要(也不应该)共享用户名和密码来共享数据存储。此行:datastoreTitle.setRole(DbxPrincipal.PUBLIC、DbxDatastore.Role.EDITOR);设置该数据存储上的权限,以便具有该数据存储的数据存储ID的任何Dropbox用户都可以对其进行编辑。他们可以也应该使用自己的帐户,并使用自己的用户名和密码登录。
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();
}
}
}
while (mRecord.hasNext()){
...