我希望我的应用用户能够查看Dropbox文件和文件夹,以便他/她可以选择其中一个并在我的应用中使用。
为此,我使用了Dropbox Core API:
private DropboxAPI<AndroidAuthSession> mDBApi;
AppKeyPair appKeys = new AppKeyPair(Constants.DROPBOX_APP_KEY, Constants.DROPBOX_APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
点击我的按钮,我开始授权:
mDBApi.getSession().startOAuth2Authentication(MainActivity.this);
并在onResume()中处理此问题,如此
if (mDBApi.getSession().authenticationSuccessful()) {
try {
// Required to complete auth, sets the access token on the session
mDBApi.getSession().finishAuthentication();
String accessToken = mDBApi.getSession().getOAuth2AccessToken();
storeAccessToken(accessToken);
setLoggedIn(true);
Log.v("DbAuthLog", "DB Authentication successful");
} catch (IllegalStateException e) {
Log.i("DbAuthLog", "Error authenticating", e);
}
}
其中setLoggedIn()是:
private void setLoggedIn(boolean isLoggedIn) {
AccessTokenPair access = new AccessTokenPair(Constants.DROPBOX_APP_KEY, Constants.DROPBOX_APP_SECRET);
mDBApi.getSession().setAccessTokenPair(access);
if (isLoggedIn) {
String[] fnames = null;
DropboxAPI.DeltaPage<DropboxAPI.Entry> dirent = null;
try {
dirent = mDBApi.delta("");
//dirent = mDBApi.metadata("/", 100, null, true, null);
ArrayList<DropboxAPI.DeltaEntry<DropboxAPI.Entry>> files = new ArrayList<>();
ArrayList<String> dir=new ArrayList<String>();
int i = 0;
for (DropboxAPI.DeltaEntry<DropboxAPI.Entry> ent: dirent.entries)
{
files.add(ent);// Add it to the list of thumbs we can choose from
//dir = new ArrayList<String>();
dir.add(new String(files.get(i++).lcPath));
}
i=0;
fnames=dir.toArray(new String[dir.size()]);
} catch (DropboxException e) {
e.printStackTrace();
}
}
}
但是这引发了异常,说'未能将结果传递给活动'。
请建议,我需要一份所有文件和文件夹的列表。
logcat的:
W/DefaultRequestDirector: Authentication error: Unable to respond to any of these challenges: {oauth=Www-Authenticate: OAuth realm="https://api.dropbox.com/"}
W/System.err: com.dropbox.client2.exception.DropboxUnlinkedException
at com.dropbox.client2.RESTUtility.parseAsJSON(RESTUtility.java:263)
at com.dropbox.client2.RESTUtility.execute(RESTUtility.java:415)
at com.dropbox.client2.RESTUtility.execute(RESTUtility.java:339)
at com.dropbox.client2.RESTUtility.streamRequest(RESTUtility.java:194)
at com.dropbox.client2.RESTUtility.request(RESTUtility.java:124)
at com.dropbox.client2.DropboxAPI.delta(DropboxAPI.java:2476)
at com.example.admin.cloudintegrationsample.MainActivity$DownloadFilesTask.doInBackground(MainActivity.java:130)
at com.example.admin.cloudintegrationsample.MainActivity$DownloadFilesTask.doInBackground(MainActivity.java:125)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
06-22 13:23:50.194 14336-15079/com.example.admin.cloudintegrationsample W/IdleConnectionHandler: Removing a connection that never existed!