I've been struggling to get Dropbox V2 to work with my app for a while now and I'm not sure what else I can do. I've basically followed the online tutorial and other sources, but I keep getting errors keeping me from progressing further. The weird thing is when I first put in the new code it worked fine. Once I started adding alerts confirming success of upload or anything else it started bugging out when trying to log in. I have a feeling the app was still logged in via the old code but I have no way to confirm this because I didn't save a copy of the project at that point. I've tried putting the code in a separate Thread but I still get errors. It's like it's not connecting to the API like it should be, but I don't know what I'm doing wrong here. I'm putting the app key for the token, is that meant to be something different? Here's my code:
try {
DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial");
client = new DbxClientV2(config, "token");
// Get current account info
FullAccount account = client.users().getCurrentAccount();
System.out.println(account.getName().getDisplayName());
// Get files and folder metadata from Dropbox root directory
ListFolderResult result = client.files().listFolder("");
while (true) {
for (Metadata metadata : result.getEntries()) {
System.out.println(metadata.getPathLower());
}
if (!result.getHasMore()) {
break;
}
result = client.files().listFolderContinue(result.getCursor());
}
} catch(DbxException e){}
And the file upload code:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Exception mException=null;
FileMetadata metadata=null;
try {
// Upload "test.txt" to Dropbox
//dialog.dismiss();
metadata = client.files().uploadBuilder("/"+title+".sheet").uploadAndFinish(fis);
} catch(DbxException e){
mException = e;
}
catch(IOException e){
mException = e;
}
if (mException != null) {
//dialog.dismiss();
final Exception finalE = mException;
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
public void run() {
Log.e(TAG, "Failed to upload file.", finalE);
Toast.makeText(MainActivity.this,
"An error has occurred",
Toast.LENGTH_SHORT)
.show();
}
});
} else if (metadata == null) {
//dialog.dismiss();
final Exception finalE = mException;
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
public void run() {
Log.e(TAG, "Failed to upload file.", finalE);
Toast.makeText(MainActivity.this,
"An error has occurred",
Toast.LENGTH_SHORT)
.show();
}
});
} else {
//dialog.dismiss();
final String message = metadata.getName() + " size " + metadata.getSize() + " modified " +
DateFormat.getDateTimeInstance().format(metadata.getClientModified());
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this,
message,
Toast.LENGTH_SHORT)
.show();
}
});
}
}
});
t.start();
Any ideas or tips would be greatly appreciated.