We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
Andre_LB
8 years agoExplorer | Level 3
v2 API
Hi Dropbox Team, I am trying, using Dropbox API, to access my files in Dropbox. I have already done it with Dropbox API v1 and now I would like to do it with v2. For this reason, I performed as fo...
Andre_LB
8 years agoExplorer | Level 3
Hi Chuck,
thank you very much for your reply and suggestion.
In the mean time, there have been a few progresses. With the attached code, I am able to start and complete the flow. Also, to store and re-use the resulting access token.
It seems to me have obtained an access token but I am not able to make a client.
Indeed, when I add the "requestConfig" setting I have the following error:-
error: cannot access OkHttpClient
class file for okhttp3.OkHttpClient not found
I hope that this time to be easer for you to help me.
But I have another issue.
When I'll have made a client, it is still not clear how may I use it.
I mean, I would like to download/upload a file from/to a Dropbox folder of mine.
Well, how can I perform it?
Thank you very much, in advance.
Kind Regards,
Andre
public class PrvDef extends AppCompatActivity {
private static DbxClientV2 sDbxClient;
public Button auth_but;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prv_def);
auth_but = (Button) findViewById(R.id.login_button);
auth_but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Auth.startOAuth2Authentication(PrvDef.this, getString(R.string.app_key));
}
});
}
@Override
protected void onResume() {
super.onResume();
if (hasToken()) {
findViewById(R.id.login_button).setVisibility(View.GONE);
findViewById(R.id.email_text).setVisibility(View.VISIBLE);
findViewById(R.id.name_text).setVisibility(View.VISIBLE);
findViewById(R.id.type_text).setVisibility(View.VISIBLE);
findViewById(R.id.files_button).setEnabled(true);
findViewById(R.id.open_with).setEnabled(true);
} else {
findViewById(R.id.login_button).setVisibility(View.VISIBLE);
findViewById(R.id.email_text).setVisibility(View.GONE);
findViewById(R.id.name_text).setVisibility(View.GONE);
findViewById(R.id.type_text).setVisibility(View.GONE);
findViewById(R.id.files_button).setEnabled(false);
findViewById(R.id.open_with).setEnabled(false);
}
}
protected boolean hasToken() {
SharedPreferences prefs = getSharedPreferences("dropbox-sample", MODE_PRIVATE);
String accessToken = prefs.getString("access-token", null);
if (accessToken == null) {
accessToken = Auth.getOAuth2Token();
if (accessToken != null) {
prefs.edit().putString("access-token", accessToken).apply();
}
}
//-- requestConfig setting --------------------------------------------------------
DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder("examples-v2-demo")
.withHttpRequestor(new OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient()))
.build();
sDbxClient = new DbxClientV2(requestConfig, accessToken);
return accessToken != null;
}
}
========================= Manifest =====================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rram.prvdef">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".PrvDef">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.dropbox.core.android.AuthActivity"
android:configChanges="orientation|keyboard"
android:launchMode="singleTask">
<intent-filter>
<!-- Change this to be db- followed by your app key -->
<data android:scheme="db-..my app key ..." />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
chirstius
Dropbox Staff
8 years ago
The issue regarding OkHttpClient not being found seems like it is a development environment issue, and not directly related to the Dropbox API. Are you getting errors from gradle or maven? Have you tried updating your project, or forcing a clean build? OkHttp is a dependency of the Dropbox SDK and should be included in your build environment when you include the Dropbox SDK. If you have checked out the SDK to build it yourself the dependency is still there and you'd likely see build errors occuring if it were not imported properly. As a test, you could try including the dependency directly (http://square.github.io/okhttp/#download) to see if it gets you past this error. But I'm not sure there is much direct help I can offer you in resolving this issue specifically since it is technically not an API issue.
As for how to upload/download a file via the Java SDK on Android there are examples available within the github respository:
Upload: https://github.com/dropbox/dropbox-sdk-java/blob/947f3f5aa9d5903a222d4b9e4ce7ce2c4defc2a4/examples/android/src/main/java/com/dropbox/core/examples/android/UploadFileTask.java#L61
I hope that gets you a bit further along,
-Chuck
- Andre_LB8 years agoExplorer | Level 3
Hi Chuck,
thank you for your prompt reply.Lovely, I included the dependency directly, as you suggested, and the error that I had from gradle has disappeared!Now, if I run the app, I have got:-Unfortunately, PrvDef has stopped.The issue seems to be in client creating command (that is, "sDbxClient = new DbxClientV2(requestConfig, accessToken);" ).
Indeed, if I comment it this issue disappears.I did not change the code (that i previously sent to you).Let me know, please, if you need more information.Thank you very much, in advance.Kind Regards,Andre- chirstius8 years ago
Dropbox Staff
Again this appears to be a more generic Android development question and not a Dropbox API specific question.
I would suggest you take a good look at the sample Android application included in the Java SDK github repository:
https://github.com/dropbox/dropbox-sdk-java/tree/master/examples/android/src/main/java/com/dropbox/core/examples/androidSpecifically, you are creating the client within your hasToken() method which seems a dubious place to perform such an action.
I suggest following the flow of the sample application carefully and paying attention to the following class:
https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/android/src/main/java/com/dropbox/core/examples/android/DropboxClientFactory.javaAnd when/where it gets invoked:
https://github.com/dropbox/dropbox-sdk-java/search?utf8=%E2%9C%93&q=DropboxClientFactory&type=
Good luck,
-Chuck
- Andre_LB8 years agoExplorer | Level 3
Hi Chuck,
thanks for your reply and for yuor advice.
I have set a project with the following classes:-
. PrvDef
. DropboxActivity
. DropboxClientFactory
. FileThumbnailRequestHandler
. GetCurrentAccountTask
. PicassoClientDropboxActivity, DropboxClientFactory, FileThumbnailRequestHandler, GetCurrentAccountTask and PicassoClient
are exactly the same of "dropbox-sdk-java-master" project classes (apart from the package declaration, of course).PrvDef class (attached) is slightly different from UserActivity: indeed I have commented the "filesButton" and "openWithButton" commands. Since I have a problem "before" them, I tried to simplify the project.
I run it, but the result is:-Unfortunately, PrvDef Has stopped.
Now, if I comment the three rows in "initAndLoadData" method of DropboxActivity class, this issue disappear.
Also, both "view files" and "Test Open" buttons are enabled, that is the filing is that hasToken() method to be true.
Since my code is now so similar to "dropbox-sdk-java-master" code, it seems to me that it is no more possible for me to improve it.
Please, let me know your opinion. Thanks, in advance.-------------------------------
Since I had a build.gradle file different from "dropbox-sdk-java-master" project one, I updated mine.
But I descovered that the build.gradle file of the "dropbox-sdk-java-master" project has Gradle errors.
I tried to remove those Gradle errors, but I have not been able to do it .I mean, I have "Failed to resolve: com.dropbox.core:dropbox-core-sdk:0-SNAPSHOT" error.
Could you please confirm me that the build.gradle file of "dropbox-sdk-java-master" project not to have got Gradle Build errors and that project to be still running in Android correctly?
Also, I noticed that the dropbox-core-sdk library is not present in "dropbox-sdk-java-master". Where may I find the right version?
Thank you, in advance.
Kind Regards,
Andre
public class PrvDef extends DropboxActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
Button loginButton = (Button)findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Auth.startOAuth2Authentication(PrvDef.this, getString(R.string.app_key));
}
});
Button filesButton = (Button)findViewById(R.id.files_button);
filesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//startActivity(FilesActivity.getIntent(PrvDef.this, ""));
}
});
Button openWithButton = (Button)findViewById(R.id.open_with);
openWithButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent openWithIntent = new Intent(UserActivity.this, OpenWithActivity.class);
//startActivity(openWithIntent);
}
});
}
@Override
protected void onResume() {
super.onResume();
if (hasToken()) {
findViewById(R.id.login_button).setVisibility(View.GONE);
findViewById(R.id.email_text).setVisibility(View.VISIBLE);
findViewById(R.id.name_text).setVisibility(View.VISIBLE);
findViewById(R.id.type_text).setVisibility(View.VISIBLE);
findViewById(R.id.files_button).setEnabled(true);
findViewById(R.id.open_with).setEnabled(true);
} else {
findViewById(R.id.login_button).setVisibility(View.VISIBLE);
findViewById(R.id.email_text).setVisibility(View.GONE);
findViewById(R.id.name_text).setVisibility(View.GONE);
findViewById(R.id.type_text).setVisibility(View.GONE);
findViewById(R.id.files_button).setEnabled(false);
findViewById(R.id.open_with).setEnabled(false);
}
}
@Override
protected void loadData() {
new GetCurrentAccountTask(DropboxClientFactory.getClient(), new GetCurrentAccountTask.Callback() {
@Override
public void onComplete(FullAccount result) {
((TextView) findViewById(R.id.email_text)).setText(result.getEmail());
((TextView) findViewById(R.id.name_text)).setText(result.getName().getDisplayName());
((TextView) findViewById(R.id.type_text)).setText(result.getAccountType().name());
}
@Override
public void onError(Exception e) {
Log.e(getClass().getName(), "Failed to get account details.", e);
}
}).execute();
}
}
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!