【问题标题】:Syncing Data with Dropbox Sync API使用 Dropbox Sync API 同步数据
【发布时间】:2013-07-26 08:59:16
【问题描述】:

我正在尝试通过 Dropbox SYNC API 在 android 中实现 Icloud 功能(在不同设备中同时下沉)。我能够读写文件到 Dropbox。但是我的问题是,即使我已经实现了DbxFile.Listener,我也无法监听 Dropbox 中文件的更改。我在这里发布我的示例代码。通过这个示例代码,我的目标是如果我在 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");

    }
};

}

【问题讨论】:

  • 您是如何从 Dropbox 下载文件的?你能指导我吗

标签: android dropbox dropbox-api


【解决方案1】:

看起来您正在添加一个侦听器,但随后立即关闭了文件。仅当您打开该文件时才会触发单个文件上的更改侦听器。

您应该保持文件打开并使用DbxFile.Listener 来监视文件更改并更新用户界面。为此,您需要查看DbxFile.getNewerStatus()。像这样的:

@Override
public void on FileChange(DbxFile paramDbxFile) {
    if (paramDbxFile.getNewerStatus().isCached) {
        paramDbxFile.update();
        readFile(dbxFs);
    }
}

当然,这意味着如果用户正在编辑文本,然后在其他地方更改了文本,用户的编辑将丢失(因为readFile 更新了EditText)。相反,您可能想告诉用户有更新的内容可用,并让他们决定是在保存时拉入新内容还是用自己的内容覆盖。

【讨论】:

  • 感谢@smarx,您指出了我的一个错误。真正的问题是我没有接到 onFile 更改侦听器的回调。是因为我要关闭文件吗?所以我该怎么做??在 onResume() 上打开文件并在 onPause() 上关闭它??
  • 除非您保持打开状态,否则您不会收到文件已更改的通知。是的,我认为您应该在onResume 中打开它并在onPause 中关闭它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多