使用dropbox dropbox api中的txt文件同步内部存储中的txt文件

时间:2013-08-30 21:15:14

标签: java android sync dropbox dropbox-api

我想在android内部存储(或sdcard)中同步一个txt文件,并让它自动同步到我的Dropbox中的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次,一切都没有发生..

1 个答案:

答案 0 :(得分:2)

getFromDbx中,您拨打startLink,这会调出用于关联帐户的用户界面。如果成功,则会执行onActivityResult getFromDbx,您拨打startLink,再次呼叫{{1}}。 :-)看起来像是一个无限循环。