我在保管箱网站上关注本教程。
我已经获得了应用程序密钥和秘密,并将它们放在我的代码中以及清单中的正确位置。清单也有互联网许可。
所以设置很好。该应用程序旨在将文本文件上传到我的保管箱帐户。它似乎开始正确地进行身份验证,但 putFile() 方法抛出了 DropboxUnlinkedException。当应用程序运行时,您必须允许在手机上访问您的帐户,当我单击允许应用程序完成正确身份验证时。我认为除了执行 putfile() 方法之外一切都很好。
我试过在应用程序启动时清除密钥,但仍然没有运气。
我做了一些日志记录,但不知道现在该做什么,有人有什么想法吗?
谢谢马特。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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.DropboxUnlinkedException;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.Session.AccessType;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class DropboxfileuploadActivity extends Activity {
private static final String TAG = "DropboxfileuploadActivity";
final static private String APP_KEY = "***********";
final static private String APP_SECRET = "k3i***********";
final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
final static private String ACCOUNT_PREFS_NAME = "prefs";
final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";
private DropboxAPI<AndroidAuthSession> mDBApi;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
clearKeys();
Log.e(TAG, "keys cleared");
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
mDBApi.getSession().startAuthentication(DropboxfileuploadActivity.this);
Log.e(TAG, "started authentication");
FileInputStream inputStream = null;
try {
File fileToUpload = new File(Environment.getExternalStorageDirectory()
+File.separator
+"dropboxapp");
fileToUpload.mkdirs();
Log.e(TAG, "dirs made");
File file = new File(fileToUpload.getAbsolutePath()+"/uploadedFile.txt");
Log.e(TAG, "the file to be uploaded has a size of "+file.length()+" bytes");
inputStream = new FileInputStream(file);
Log.e(TAG, "inputstream created");
Entry newEntry = mDBApi.putFile("/test.txt", inputStream,
file.length(), null, null);
Log.e(TAG, "putFile method executed");
Log.i("DbExampleLog", "The uploaded file's rev is: " + newEntry.rev);
} catch (DropboxUnlinkedException e) {
// User has unlinked, ask them to link again here.
Log.e("DbExampleLog", "User has unlinked.");
} catch (DropboxException e) {
Log.e("DbExampleLog", "Something went wrong while uploading.");
} catch (FileNotFoundException e) {
Log.e("DbExampleLog", "File not found.");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {}
}
}
setContentView(R.layout.main);
}//end of oncreate()
protected void onResume() {
super.onResume();
if (mDBApi.getSession().authenticationSuccessful()) {
try {
// MANDATORY call to complete auth.
// Sets the access token on the session
mDBApi.getSession().finishAuthentication();
if(mDBApi.getSession().authenticationSuccessful()){
Log.e(TAG, "Authentication finished");
}
AccessTokenPair tokens = mDBApi.getSession().getAccessTokenPair();
// Provide your own storeKeys to persist the access token pair
// A typical way to store tokens is using SharedPreferences
storeKeys(tokens.key, tokens.secret);
} catch (IllegalStateException e) {
Log.i("DbAuthLog", "Error authenticating", e);
}
}
}//end of onResume()
private void storeKeys(String key, String secret) {
// Save the access key for later
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.putString(ACCESS_KEY_NAME, key);
edit.putString(ACCESS_SECRET_NAME, secret);
edit.commit();
}
private void clearKeys() {
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.clear();
edit.commit();
}
}//end of class
.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tecmark.dropboxfileupload"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".DropboxfileuploadActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:launchMode="singleTask"
android:configChanges="orientation|keyboard">
<intent-filter>
<!-- Change this to be db- followed by your app key -->
<data android:scheme="db-3*********" />
<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>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
.
06-03 09:54:51.117: E/DropboxfileuploadActivity(20365): started authentication
06-03 09:54:51.117: E/DropboxfileuploadActivity(20365): dirs made
06-03 09:54:51.117: E/DropboxfileuploadActivity(20365): the file to be uploaded has a size of 147 bytes
06-03 09:54:51.117: E/DropboxfileuploadActivity(20365): inputstream created
06-03 09:54:51.117: E/DbExampleLog(20365): User has unlinked.
06-03 09:54:57.397: E/DropboxfileuploadActivity(20365): Authentication finished
.
这是完整的堆栈。
06-03 09:54:51.067: E/DropboxfileuploadActivity(20365): keys cleared
06-03 09:54:51.077: E/dalvikvm(20365): Could not find class 'org.json.simple.JSONArray', referenced from method com.dropbox.client2.DropboxAPI.revisions
06-03 09:54:51.077: W/dalvikvm(20365): VFY: unable to resolve check-cast 223 (Lorg/json/simple/JSONArray;) in Lcom/dropbox/client2/DropboxAPI;
06-03 09:54:51.087: D/dalvikvm(20365): VFY: replacing opcode 0x1f at 0x0053
06-03 09:54:51.087: E/dalvikvm(20365): Could not find class 'org.json.simple.JSONArray', referenced from method com.dropbox.client2.DropboxAPI.search
06-03 09:54:51.087: W/dalvikvm(20365): VFY: unable to resolve instanceof 223 (Lorg/json/simple/JSONArray;) in Lcom/dropbox/client2/DropboxAPI;
06-03 09:54:51.087: D/dalvikvm(20365): VFY: replacing opcode 0x20 at 0x006d
06-03 09:54:51.117: E/DropboxfileuploadActivity(20365): started authentication
06-03 09:54:51.117: E/DropboxfileuploadActivity(20365): dirs made
06-03 09:54:51.117: E/DropboxfileuploadActivity(20365): the file to be uploaded has a size of 147 bytes
06-03 09:54:51.117: E/DropboxfileuploadActivity(20365): inputstream created
06-03 09:54:51.117: E/DbExampleLog(20365): User has unlinked.
06-03 09:54:57.397: E/DropboxfileuploadActivity(20365): Authentication finished
06-03 10:21:31.087: W/IInputConnectionWrapper(20365): showStatusIcon on inactive InputConnection
06-03 10:21:31.087: W/IInputConnectionWrapper(20365): InputConnection = android.view.inputmethod.BaseInputConnection@40d727e0, active client = false