Dropbox Sync API startLink()方法无法正常工作

时间:2013-07-15 08:43:28

标签: android dropbox-api

我正在开发一款使用Dropbox Sync API上传文件的Android应用。我已经在Dropbox上创建了应用程序,获得了APP_KEY和APP_SECRET。我已经包含了所有必要的库,在我的活动代码和Manifest中设置了正确的密钥。我的应用程序类似于文档中提供的HelloDropbox示例,但当我点击“链接到Dropbox”按钮时,该按钮应显示输入我的Dropbox凭据的位置,但没有任何反应。这是源代码:

package com.diamondtrust66.helix.player;
import java.io.File;
import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.sync.android.DbxAccountManager;
import com.dropbox.sync.android.DbxFile;
import com.dropbox.sync.android.DbxFileInfo;
import com.dropbox.sync.android.DbxFileSystem;
import com.dropbox.sync.android.DbxPath;

public class HelixPlayer extends Activity {

private static final String appKey = "1234-my-key";
private static final String appSecret = "1234-my-secret";

private static final int REQUEST_LINK_TO_DBX = 0;

private TextView mTestOutput;
private Button mLinkButton;
private DbxAccountManager mDbxAcctMgr;
private DropboxAPI<?> mDBApi;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_helix_player);
    mTestOutput = (TextView) findViewById(R.id.test_output);
    mLinkButton = (Button) findViewById(R.id.link_button);
    mLinkButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onClickLinkToDropbox();
        }
    });

    mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);
}

@Override
protected void onResume() {
    super.onResume();
    if (mDbxAcctMgr.hasLinkedAccount()) {
        showLinkedView();
        doDropboxTest();
    } else {
        showUnlinkedView();
    }
}

private void showLinkedView() {
    mLinkButton.setVisibility(View.GONE);
    mTestOutput.setVisibility(View.VISIBLE);
}

private void showUnlinkedView() {
    mLinkButton.setVisibility(View.VISIBLE);
    mTestOutput.setVisibility(View.GONE);
}

private void onClickLinkToDropbox() {
    mDbxAcctMgr.startLink((Activity)this, REQUEST_LINK_TO_DBX);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_LINK_TO_DBX) {
        if (resultCode == Activity.RESULT_OK) {
            doDropboxTest();
        } else {
            Toast.makeText(getApplicationContext(), "FAILURE", Toast.LENGTH_LONG).show();
            mTestOutput.setText("Link to Dropbox failed or was cancelled.");
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

private void doDropboxTest() {
    try {
        final String TEST_DATA = "Hello Dropbox";
        final String TEST_FILE_NAME = "be like that.mp3";
        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.
        List<DbxFileInfo> infos = dbxFs.listFolder(DbxPath.ROOT);
        mTestOutput.setText("\nContents of app folder:\n");
        for (DbxFileInfo info : infos) {
            mTestOutput.append("    " + info.path + ", " + info.modifiedTime + '\n');
        }

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

                File myFile = new File("/mnt/sdcard/alarms/be like that.mp3");
                //testFile.writeString(TEST_DATA);
                testFile.writeFromExistingFile(myFile, false);
            } finally {
                testFile.close();
            }
            mTestOutput.append("\nCreated new file '" + testPath + "'.\n");
        }

        // 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();
            }
            mTestOutput.append("\nRead file '" + testPath + "' and got data:\n    " + resultData);
        } else if (dbxFs.isFolder(testPath)) {
            mTestOutput.append("'" + testPath.toString() + "' is a folder.\n");
        }*/
    } catch (IOException e) {
        mTestOutput.setText("Dropbox test failed: " + e);
    }
}

}

2 个答案:

答案 0 :(得分:0)

您是否能够在遇到此问题的同一模拟器/设备上运行未修改的Hello Dropbox示例?您也可以尝试使用自己的替换样本中的app key / secret。如果这些也失败了,那么设备配置可能会出现问题,导致API无法启动浏览器以完成身份验证。如果该示例有效,但您的应用没有,那么我怀疑那里配置错误。你能用日志声明检查你对startLink()的调用是否真的发生了吗?你知道在那之后LogCat中出现了什么吗?

进一步调试此方法的最佳方法可能是打开支持票证。使用此处的API支持链接:https://www.dropbox.com/developers

答案 1 :(得分:0)

我遇到了同样的问题,当我尝试使用与我的设备安装的另一个Android应用程序相同的Dropbox应用程序凭据时,startLink()没有做任何事情(虽然没有运行)但是它没有用。所以你有两个选择:使用相同的凭据卸载任何其他Android应用程序或创建另一个Dropbox应用程序并更新app / pass键集。只有这样才会出现Dropbox登录对话框。

相关问题