Integrating Dropbox with Android is very simple. Just follow this step-by-step tutorial and you will be ready. In this tutorial we will see how to integrate Dropbox API in Android application and also how to retrieve the list of folders from Dropbox
- Go to https://www.dropbox.com/developers If you already have Dropbox account then sign-in with your credentials else just signup for a new account. Once you login, you will see the following screen
- Now click on App Console. Here you can see the list of apps you have created (if any). Click on the Create app button
- Dropbox provides different types of API's.In this tutorial we will see Core API which provides access to user's full dropbox. On the next screen select Dropbox API app
- When you select Dropbox API app you will be prompted with different questions. Select the choices as shown in the below screenshot
- Next provide a name for your app and click on create app button
- Once your app is created you will see the settings page where you can configure additional parameters for your app. Please take a note of App Key and App Secret. For security reasons I just hid them
- Next goto https://www.dropbox.com/developers/core/sdks/android and download the latest Dropbox Android SDK
- Now create a new Android project called DropboxDemo. In Eclipse goto File [icon name="icon-angle-right"] New [icon name="icon-angle-right"] Android Application Project. Fill in all the details and click finish to create the project
- Extract the ZIP file you have previously downloaded, goto lib directory, copy and paste the dropbox-android-sdk.jar and json_simple.jar into the libs folder of your android project
- Goto res [icon name="icon-angle-right"] layout [icon name="icon-angle-right"] main.xml and paste the following code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/directory_list" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout>
- Now open DropboxDemo.java file and paste the following code. YOUR_APP_KEY and YOUR_APP_SECRET must be replaced with your API key and API Secret
package com.yogasaikrishna.dropboxdemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.ListView; import com.dropbox.client2.DropboxAPI; import com.dropbox.client2.DropboxAPI.Entry; import com.dropbox.client2.android.AndroidAuthSession; import com.dropbox.client2.exception.DropboxException; import com.dropbox.client2.exception.DropboxPartialFileException; import com.dropbox.client2.exception.DropboxServerException; import com.dropbox.client2.exception.DropboxUnlinkedException; import com.dropbox.client2.session.AppKeyPair; public class DropboxDemo extends Activity { private static final String APP_KEY = "YOUR_APP_KEY"; private static final String APP_SECRET = "YOUR_APP_SECRET"; private DropboxAPI<AndroidAuthSession> mDBApi; private AppKeyPair mAppKeys; private AndroidAuthSession mSession; private ListView mDirectoryList; private ArrayAdapter<String> mDirectoryAdapter; private List<String> mDir; private ProgressDialog mDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mAppKeys = new AppKeyPair(APP_KEY, APP_SECRET); mSession = new AndroidAuthSession(mAppKeys); mDBApi = new DropboxAPI<AndroidAuthSession>(mSession); mDBApi.getSession().startOAuth2Authentication(DropboxDemo.this); mDirectoryList = (ListView) findViewById(R.id.directory_list); } @Override protected void onResume() { super.onResume(); if (mDBApi.getSession().authenticationSuccessful()) { try { mDBApi.getSession().finishAuthentication(); new SyncDirectories().execute(); } catch (IllegalStateException e) { Log.d("DropboxDemo", "Error Authenticating..."); } } } private class SyncDirectories extends AsyncTask<Void, Void, Boolean> { @Override protected void onPreExecute() { super.onPreExecute(); mDir = new ArrayList<String>(); mDialog = ProgressDialog.show(DropboxDemo.this, "", "Loading..."); } @Override protected Boolean doInBackground(Void... arg0) { try { Entry dirent = mDBApi.metadata("", 25000, null, true, null); for (Entry ent : dirent.contents) { if (ent.isDir) { mDir.add(ent.fileName()); } } } catch (DropboxUnlinkedException e) { Log.d("DropboxDemo", "An Exception Occurred.."); } catch (DropboxPartialFileException e) { Log.d("DropboxDemo", "An Exception Occurred.."); } catch (DropboxServerException e) { if (e.error == DropboxServerException._304_NOT_MODIFIED) { } else if (e.error == DropboxServerException._401_UNAUTHORIZED) { } else if (e.error == DropboxServerException._403_FORBIDDEN) { } else if (e.error == DropboxServerException._404_NOT_FOUND) { } else if (e.error == DropboxServerException._406_NOT_ACCEPTABLE) { } else if (e.error == DropboxServerException._415_UNSUPPORTED_MEDIA) { } else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) { } else { } Log.d("DropboxDemo", e.body.error); } catch (DropboxException e) { Log.d("DropboxDemo", "An Exception Occurred.."); } return false; } @Override public void onPostExecute(Boolean result) { mDirectoryAdapter = new ArrayAdapter<String>(DropboxDemo.this, android.R.layout.simple_list_item_1, mDir); mDirectoryList.setAdapter(mDirectoryAdapter); mDialog.dismiss(); } } }
- Now open AndroidManifest.xml file and paste the following code. You have to add INTERNET permission and Dropbox auth Activity to the manifest file. YOUR-APP-KEY must be replaced with your API key
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yogasaikrishna.dropboxdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.yogasaikrishna.dropboxdemo.DropboxDemo" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Dropbox API related code --> <activity android:name="com.dropbox.client2.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-YOUR-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>
- Now you are ready to go. Launch the application in Emulator or Android device. Click allow when prompted and you will see the list the folders in your dropbox account
0 Komentar untuk "Integrating Dropbox API in Android Application"