问题
I want my app user to be able to view Dropbox files and folders, so that he/she can choose one of them and use in my app.
For this I used Dropbox Core API like this:
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);
On my button click, I started authorization:
mDBApi.getSession().startOAuth2Authentication(MainActivity.this);
and handled this in onResume(), like this
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);
}
}
where setLoggedIn() is:
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();
}
}
}
But this throws exception, saying 'Failure delivering result to activity'.
Please suggest, I need a list of all files and folders.
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!
来源:https://stackoverflow.com/questions/37961423/get-all-files-and-folders-list-with-dropbox-core-api-for-android