The Android SDK depends on the Box Java SDK, so you must first import it into your workspace and make sure it builds. Import the Android SDK second and make the Java SDK a build dependency.
-
- In your workspace, clone java sdk: git clone [email protected]:box/box-java-sdk-v2.git
-
- In your workspace, clone android sdk: git clone [email protected]:box/box-android-sdk-v2.git
-
- In eclipse, import the two projects. File->Import->General->Existing Projects into Workspace, then select these two projects.
-
- Copy jar files in java sdk library to your android project.
- If you don't have a "libs" folder under your android project root folder, create one.
- In "libs" folder(BoxJavaLibraryV2/libs) of java sdk project, copy the non-testing jar files into your android libs folder. They are: commons*.jar, http*.jar, jackson*.jar.
-
- Add box android sdk as an android library project: Open properties of your project, select "Android", on bottom "Library" section, click "Add...", add BoxAndroidLibraryV2.
-
- Resolve possible conflicts. In case your project already referred to different version of a jar our sdk refers, there would be a conflict. Choose the best version and replace the jar in both projects. We recommend you always use the latest version, or the version that's more stable if you know.
-
- Follow the instructions in Box Java SDK's readme on how to build it.
-
- Copy the the built BoxJavaLibraryV2.jar to BoxAndroidLibraryV2/libs.
-
- You can then use Ant to build the project like you
would with any other Android library. The simplest way to do this is by running
ant debug
.
- You can then use Ant to build the project like you
would with any other Android library. The simplest way to do this is by running
-
- Resolve possible conflict, same as step 6 in eclipse build instruction.
There is also experimental support for Gradle, allowing you to use the SDK with Android Studio. You must have Gradle 1.6 installed.
Before the Android SDK can be built, you must first install the Box Java SDK to your local Maven repository. This can be done by following the Gradle build instructions included in the Java SDK's readme.
The Android SDK also depends on the Android Support Library. Unfortunately, telling Gradle to look for the android-support JAR directly will likely result in dex merge conflicts if more than one project uses the support library. The easiest way to get around this is by also installing android-support-v4.jar to your local Maven repo. Run the following command, where $ANDROID_HOME points to your Android SDK root (you must have Maven installed).
mvn install:install-file \
-Dfile=$ANDROID_HOME/extras/android/support/v4/android-support-v4.jar \
-DgroupId=com.google.android \
-DartifactId=support-v4 \
-Dversion=r13 \
-Dpackaging=jar
You can now run gradle build
to build the SDK. However, building the library
by itself isn't very useful. To reference the SDK from another Android Gradle
project, add the following to your list of dependencies:
dependencies {
...
compile project(':box-android-sdk-private:BoxAndroidLibraryV2')
}
You can refer to the Android Gradle guide on multi project setups here .
Here is a more detailed tutorial on setting up box sdk using gradle.
Make sure you've set up your client id, client secret and (optional) redirect url correctly. Please refer to [developer document] (http://developers.box.com/oauth/) for more information. You can find a full example of how to perform authentication in the sample app.
The easiest way to authenticate is to use the OAuthActivity, which is included in the SDK. Add it to your manifest to use it.
// If you don't have a server redirect url, use this instead:
// Intent intent = createOAuthActivityIntent(context, clientId, clientSecret, false, "http://localhost");
Intent intent = OAuthActivity.createOAuthActivityIntent(this, clientId,
clientSecret);
startActivityForResult(intent);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_CANCELED) {
// Get the error message for why authentication failed.
String failMessage = data.getStringExtra(OAuthActivity.ERROR_MESSAGE);
// Implement your own logic to handle the error.
handleFail(failMessage);
} else {
// You will get an authenticated oath token object back upon success.
BoxAndroidOAuthData oauth = data.getParcelableExtra(OAuthActivity.BOX_CLIENT_OAUTH);
// If you don't want to supply a customized hub or parser, use null to fall back to defaults.
BoxAndroidClient client = new BoxAndroidClient(clientId, clientSecret, null, null);
client.authenticate(oauth);
youOwnMethod(client);
}
}
Our sdk auto refreshes OAuth access token when it expires. You will want to listen to the refresh events and update your stored token after refreshing.
boxClient.addOAuthRefreshListener(OAuthRefreshListener listener) {
new OAuthRefreshListener() {
@Override
public void onRefresh(IAuthData newAuthData) {
BoxOAuthToken oauthObject = boxClient.getAuthData();
// TODO: save the auth data.
}
}
}
Alternatively, you can use your own custom login activity with a WebView for authentication.
oauthView = (OAuthWebView) findViewById(R.id.oauthview);
oauthView.initializeAuthFlow(this, clientId, clientSecret);
boxClient.authenticate(oauthView, autoRefreshOAuth, getOAuthFlowListener());
// Create a listener listening to OAuth flow. The most important part you need
// to implement is onAuthFlowEvent and catch the OAUTH_CREATED event. This event
// indicates that the OAuth flow is done, the BoxClient is authenticated and
// that you can start making API calls.
private OAuthWebViewListener getOAuthFlowListener() {
return new OAuthWebViewListener() {
@Override
public onAuthFlowEvent(final IAuthEvent event,
final IAuthFlowMessage message) {
// Authentication is done, you can start using your BoxClient
// instance.
}
}
}
boxClient.addOAuthRefreshListener(OAuthRefreshListener listener) {
new OAuthRefreshListener() {
@Override
public void onRefresh(IAuthData newAuthData) {
BoxOAuthToken oauthObject = boxClient.getAuthData();
// TODO: save the auth data.
}
}
}
After you exit the app and return back, you can use the stored oauth data to authenticate:
// Re-authenticate using the previously obtained OAuth object.
boxClient.authenticate(oauthObject);
BoxFile boxFile = boxClient.getFilesManager().getFile(fileId, null);
Get default file info plus its description and SHA1.
BoxDefaultRequestObject requestObj =
(new BoxDefaultRequestObject()).addField(BoxFile.FIELD_SHA1);
.addField(BoxFile.FIELD_DESCRIPTION);
BoxFile boxFile = boxClient.getFilesManager().getFile(fileId, requestObj);
Get 30 child items, starting from the 20th one, requiring etag, description, and name to be included.
BoxFolderRequestObject requestObj =
BoxFolderRequestObject.getFolderItemsRequestObject(30, 20)
.addField(BoxFolder.FIELD_NAME)
.addField(BoxFolder.FIELD_DESCRIPTION)
.addField(BoxFolder.FIELD_ETAG);
BoxCollection collection =
boxClient.getFoldersManager().getFolderItems(folderId, requestObj);
BoxFileUploadRequestObject requestObj =
BoxFileUploadRequestObject.uploadFileRequestObject(parent, "name"�, file);
BoxFile bFile = boxClient.getFilesManager().uploadFile(requestObj);
BoxFileUploadRequestObject requestObj =
BoxFileUploadRequestObject.uploadFileRequestObject(parent, "name", file)
.setListener(listener));
BoxFile bFile = boxClient.getFilesManager().uploadFile(requestObj);
boxClient.getFilesManager().downloadFile(fileId, null);
Delete a file, but only if the etag matches.
BoxFileRequestObject requestObj =
BoxFileRequestObject.deleteFileRequestObject().setIfMatch(etag);
boxClient.deleteFile(fileId, requestObj);
Copyright 2014 Box, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.