【问题标题】:sync txt file in internal storage with txt file in dropbox dropbox api将内部存储中的 txt 文件与保管箱保管箱 api 中的 txt 文件同步
【发布时间】:2013-08-30 21:15:13
【问题描述】:

我想在 android 内部存储(或 sdcard)中同步一个 txt 文件,并让它自动同步到我的保管箱中的一个 txt。我使用了 dropbox api,但不知道如何实现这一点。需要帮助。

更新:

这是我现在将 txt 内容获取到 LogCat 的函数:

public void getFromDbx(){

        try {
            mDbxAcctMgr.startLink((Activity)this, REQUEST_LINK_TO_DBX);

            final String TEST_DATA = " Database does not exist yet";

            final String TEST_FILE_NAME = "data.txt";
            DbxPath testPath = new DbxPath(DbxPath.ROOT, TEST_FILE_NAME);

            // Create DbxFileSystem for synchronized file access.
            DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());

            // Print the contents of the root folder.  This will block until we can
            // sync metadata the first time.

            //dont need this code -- ahmed

            // Create a test file only if it doesn't already exist.
            if (!dbxFs.exists(testPath)) {
                DbxFile testFile = dbxFs.create(testPath);
                try {
                    testFile.writeString(TEST_DATA);
                } finally {
                    testFile.close();
                }

            }

            // Read and print the contents of test file.  Since we're not making
            // any attempt to wait for the latest version, this may print an
            // older cached version.  Use getSyncStatus() and/or a listener to
            // check for a new version.
            if (dbxFs.isFile(testPath)) {
                String resultData;
                DbxFile testFile = dbxFs.open(testPath);
                try {
                    resultData = testFile.readString();
                } finally {
                    testFile.close();
                }

                Log.i("dbx",resultData);
            } else if (dbxFs.isFolder(testPath)) {

            }
        } catch (IOException e) {

        }

我的 OnCreate:

super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);

        //connect dbx to account
                mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);

                //get data from ems_data.txt from dropbox account
                getFromDbx();

我的 OnActivityResult:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_LINK_TO_DBX) {
            if (resultCode == Activity.RESULT_OK) {
                getFromDbx();
            } else {
                Log.i("dbxems", "Link to Dropbox failed or was cancelled.");
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testdatabaseactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CALL_PHONE" >

    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testdatabaseactivity.TestDatabaseActivity"
            android:label="@string/app_name" >
            <intent-filter>
            </intent-filter>
        </activity>

        <instrumentation
            android:name="android.test.InstrumentationTestRunner"
            android:label="your tests label"
            android:targetPackage="com.example.testdatabaseactivity" />

        <uses-library android:name="android.test.runner" />

        <activity
            android:name="com.example.testdatabaseactivity.MainScreenActivity"
            android:label="@string/title_activity_main_screen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.testdatabaseactivity.SearchByName"
            android:label="@string/title_activity_search_by_name"
            android:windowSoftInputMode="stateHidden" >
        </activity>

        <activity android:name="com.dropbox.sync.android.DbxAuthActivity" />
        <activity
            android:name="com.dropbox.client2.android.AuthActivity"
            android:launchMode="singleTask" >
            <intent-filter>
                <data android:scheme="db-KEY_HERE" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <service
            android:name="com.dropbox.sync.android.DbxSyncService"
            android:enabled="true"
            android:exported="false"
            android:label="Dropbox Sync" />
    </application>

</manifest>

编辑:没有错误,应用程序显示身份验证屏幕,我单击允许,然后没有任何反应。我可以按允许 10 次,但什么都没有发生..

【问题讨论】:

  • 恐怕这里没什么好说的了。您能分享一下您目前编写的代码以及哪里出了问题吗?
  • @smarx-done,希望能帮到你:)
  • 你说你得到了一个java.lang.NullPointerException。您在代码中的什么地方得到了该异常?
  • 那是错误的,我看错了我的 logcat,没有错误,我已经相应地编辑了帖子。我很抱歉。

标签: java android sync dropbox dropbox-api


【解决方案1】:

getFromDbx 中,您调用startLink,这会调出用户界面以链接帐户。当成功时,你的onActivityResult 被执行,你调用getFromDbx,它再次调用startLink。 :-) 对我来说看起来像是一个无限循环。

【讨论】:

  • smarx- 感谢您的回复。这很有意义。我从教程示例中获取了这段代码,这也是那里发生的事情。我会调查一下,与此同时,我可以对代码进行任何编辑以使其工作吗?谢谢。
  • smarx- 所以我将 startLink 调用从 getFromDbx 移到 oncreate 中,现在屏幕不显示,但在 onActivityResult 中它转到“else”并打印出 Link to Dropbox Failed or was cancelled ...
  • 你能告诉我你在学习什么教程吗?如果它有错误,我想确保修复它。
  • 您能否更新问题中的代码以匹配您现在正在做的事情?
  • smarx- 感谢您的帮助。我按照您告诉我的操作修复了它,然后我错过了一件重要的事情:获取文件信息,显然您需要在从 dropbox api 中的文件中获取字符串之前执行此操作;非常感谢你:)
猜你喜欢
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多