私はドロップボックスのウェブサイトでこのチュートリアルに従っています。
アプリのキーとシークレットを取得し、それらをコードに配置し、マニフェストの正しい場所にも配置しました。また、マニフェストにもインターネット許可があります。
というわけで設定はバッチリ。このアプリは、ドロップボックス アカウントにテキスト ファイルをアップロードするためのものです。正常に認証を開始しているように見えますが、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