Android 使用Dropbox同步API同步数据
我正试图通过Dropbox SYNC API在android中实现Android 使用Dropbox同步API同步数据,android,dropbox,dropbox-api,Android,Dropbox,Dropbox Api,我正试图通过Dropbox SYNC API在android中实现Icloud功能(在不同设备中同时下沉)。我能够读写文件到Dropbox。但我的问题是,我无法监听Dropbox中文件的更改,即使我已经实现了DbxFile.Listener。我在这里张贴我的样本代码。通过这个示例代码,我的目标是如果我修改了Dropbox中的一个文件,它也应该反映在应用程序中。此外,在两台设备上运行此应用程序时,Dropbox文件夹中会发生文件冲突。谁能告诉我哪里出了问题??这方面的任何帮助都是非常值得赞赏的 p
Icloud
功能(在不同设备中同时下沉)。我能够读写文件到Dropbox。但我的问题是,我无法监听Dropbox中文件的更改,即使我已经实现了DbxFile.Listener
。我在这里张贴我的样本代码。通过这个示例代码,我的目标是如果我修改了Dropbox中的一个文件,它也应该反映在应用程序中。此外,在两台设备上运行此应用程序时,Dropbox文件夹中会发生文件冲突。谁能告诉我哪里出了问题??这方面的任何帮助都是非常值得赞赏的
public class SecondActivity extends Activity implements OnClickListener {
private DbxAccountManager mDbxAcctMgr;
private static final String APP_KEY = "xxxxxxxxx";
private static final String APP_SECRET = "xxxxxxxxxx";
DbxFile testFile;
DbxFileSystem dbxFs;
DbxPath testPath;
Button btn;
EditText edit;
String contents;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
edit = (EditText) findViewById(R.id.editText1);
btn.setOnClickListener(this);
mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(),
APP_KEY, APP_SECRET);
testPath = new DbxPath(DbxPath.ROOT, "mytext.txt");
if (!mDbxAcctMgr.hasLinkedAccount()) {
mDbxAcctMgr.startLink((Activity) this, 0);
} else {
try {
dbxFs = DbxFileSystem
.forAccount(mDbxAcctMgr.getLinkedAccount());
readFile(dbxFs);
} catch (Unauthorized e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == Activity.RESULT_OK) {
try {
dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr
.getLinkedAccount());
readFile(dbxFs);
} catch (Unauthorized e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
private void readFile(DbxFileSystem dbxFs2) {
try {
try {
if (dbxFs2.exists(testPath)) {
testFile = dbxFs.open(testPath);
testFile.addListener(listener);
String contenString = testFile.readString().toString();
showtoast(contenString);
} else {
testFile = dbxFs.create(testPath);
}
} catch (DbxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} finally {
testFile.close();
}
}
private void showtoast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
contents = edit.getText().toString();
if (!TextUtils.isEmpty(contents)) {
try {
if (dbxFs.exists(testPath)) {
new syncDropBox().execute();
} else {
dbxFs.create(testPath);
}
} catch (DbxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
testFile.close();
}
}
}
private class syncDropBox extends AsyncTask<Void, Void, Boolean> {
boolean result = false;
@Override
protected Boolean doInBackground(Void... paramArrayOfParams) {
try {
dbxFs.syncNowAndWait();
result = true;
} catch (DbxException e) {
result = false;
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
showtoast("sync succeed");
try {
testFile = dbxFs.open(testPath);
} catch (DbxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testFile.writeString(contents);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
showtoast("sync failed");
}
super.onPostExecute(result);
}
}
DbxFile.Listener listener = new Listener() {
@Override
public void onFileChange(DbxFile paramDbxFile) {
showtoast("file contents modified");
}
};
公共类SecondActivity扩展活动实现OnClickListener{
私有DbxAccountManager mDbxAcctMgr;
私有静态最终字符串APP_KEY=“xxxxxxxxx”;
私有静态最终字符串APP_SECRET=“xxxxxxxxx”;
DbxFile测试文件;
DbxFileSystem-dbxFs;
DbxPath测试路径;
按钮btn;
编辑文本编辑;
字符串内容;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(按钮)findViewById(R.id.button1);
edit=(EditText)findViewById(R.id.editText1);
btn.setOnClickListener(此);
mDbxAcctMgr=DbxAccountManager.getInstance(getApplicationContext(),
应用程序密钥、应用程序密钥);
testPath=newdbxpath(DbxPath.ROOT,“mytext.txt”);
如果(!mDbxAcctMgr.hasLinkedAccount()){
mDbxAcctMgr.startLink((活动)this,0);
}否则{
试一试{
dbxFs=DbxFileSystem
.forAccount(mDbxAcctMgr.getLinkedAccount());
readFile(dbxFs);
}渔获物(e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==0){
if(resultCode==Activity.RESULT\u确定){
试一试{
dbxFs=DbxFileSystem.forAccount(mDbxAcctMgr
.getLinkedAccount());
readFile(dbxFs);
}渔获物(e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}否则{
}
}否则{
super.onActivityResult(请求代码、结果代码、数据);
}
}
私有void读取文件(DbxFileSystem dbxFs2){
试一试{
试一试{
if(dbxFs2.exists(testPath)){
testFile=dbxFs.open(testPath);
addListener(监听器);
String contenString=testFile.readString().toString();
showtoots(contensting);
}否则{
testFile=dbxFs.create(testPath);
}
}捕获(DbxException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}最后{
testFile.close();
}
}
私有void showtoos(字符串msg){
Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
}
@凌驾
公共void onClick(视图v){
contents=edit.getText().toString();
如果(!TextUtils.isEmpty(contents)){
试一试{
if(dbxFs.exists(testPath)){
新建syncDropBox().execute();
}否则{
create(testPath);
}
}捕获(DbxException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
testFile.close();
}
}
}
私有类syncDropBox扩展了AsyncTask{
布尔结果=假;
@凌驾
受保护的布尔doInBackground(Void…paramArrayOfParams){
试一试{
syncNowAndWait();
结果=真;
}捕获(DbxException e){
结果=假;
e、 printStackTrace();
}
返回结果;
}
@凌驾
受保护的void onPostExecute(布尔结果){
如果(结果){
showtoast(“同步成功”);
试一试{
testFile=dbxFs.open(testPath);
}捕获(DbxException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
testFile.writeString(内容);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}否则{
showtoast(“同步失败”);
}
super.onPostExecute(结果);
}
}
DbxFile.Listener=newlistener(){
@凌驾
公共void onFileChange(DbxFile参数DbxFile){
showtoast(“文件内容已修改”);
}
};
}看起来您正在添加一个侦听器,但随后立即关闭该文件。更改单个文件的侦听器仅在打开该文件时触发 您应该保持文件打开,并使用
DbxFile.Listener
监视文件更改并更新UI。为此,您需要查看DbxFile.getNewerStatus()
。大概是这样的:
@Override
public void on FileChange(DbxFile paramDbxFile) {
if (paramDbxFile.getNewerStatus().isCached) {
paramDbxFile.update();
readFile(dbxFs);
}
}
当然,这意味着如果用户在编辑文本的中间,然后文本在其他地方被改变,用户的编辑将会丢失(因为<代码> Read Frase<代码>更新了<代码>编辑文本< /代码>)。您可能想告诉用户有更新的内容可用,并让他们在保存时决定是拉入新内容还是用自己的内容覆盖新内容。
谢谢@smarx,您已经指出了我的一个错误。真正的问题是我没有接到onFile changed listener的电话。是因为我要关闭文件吗??那我该怎么办??在onResume()上打开文件,并在onPause()上关闭文件??除非保持打开状态,否则不会收到文件已更改的通知。是的,我想你应该在办公室里打开它