Java 将内部存储器中的txt文件与dropbox api中的txt文件同步
我想在android内部存储器(或者SD卡)中同步一个txt文件,并让它自动同步到dropbox中的txt文件。我已经使用了dropbox api,但没有掌握如何实现这一点。需要帮助的请 更新: 下面是我现在将txt内容发送到LogCat的函数:Java 将内部存储器中的txt文件与dropbox api中的txt文件同步,java,android,sync,dropbox,dropbox-api,Java,Android,Sync,Dropbox,Dropbox Api,我想在android内部存储器(或者SD卡)中同步一个txt文件,并让它自动同步到dropbox中的txt文件。我已经使用了dropbox api,但没有掌握如何实现这一点。需要帮助的请 更新: 下面是我现在将txt内容发送到LogCat的函数: public void getFromDbx(){ try { mDbxAcctMgr.startLink((Activity)this, REQUEST_LINK_TO_DBX);
public void getFromDbx(){
try {
mDbxAcctMgr.startLink((Activity)this, REQUEST_LINK_TO_DBX);
final String TEST_DATA = " Database does not exist yet";
final String TEST_FILE_NAME = "data.txt";
DbxPath testPath = new DbxPath(DbxPath.ROOT, TEST_FILE_NAME);
// Create DbxFileSystem for synchronized file access.
DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());
// Print the contents of the root folder. This will block until we can
// sync metadata the first time.
//dont need this code -- ahmed
// Create a test file only if it doesn't already exist.
if (!dbxFs.exists(testPath)) {
DbxFile testFile = dbxFs.create(testPath);
try {
testFile.writeString(TEST_DATA);
} finally {
testFile.close();
}
}
// Read and print the contents of test file. Since we're not making
// any attempt to wait for the latest version, this may print an
// older cached version. Use getSyncStatus() and/or a listener to
// check for a new version.
if (dbxFs.isFile(testPath)) {
String resultData;
DbxFile testFile = dbxFs.open(testPath);
try {
resultData = testFile.readString();
} finally {
testFile.close();
}
Log.i("dbx",resultData);
} else if (dbxFs.isFolder(testPath)) {
}
} catch (IOException e) {
}
我的OnCreate:
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
//connect dbx to account
mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);
//get data from ems_data.txt from dropbox account
getFromDbx();
我的OnActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_LINK_TO_DBX) {
if (resultCode == Activity.RESULT_OK) {
getFromDbx();
} else {
Log.i("dbxems", "Link to Dropbox failed or was cancelled.");
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testdatabaseactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testdatabaseactivity.TestDatabaseActivity"
android:label="@string/app_name" >
<intent-filter>
</intent-filter>
</activity>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="your tests label"
android:targetPackage="com.example.testdatabaseactivity" />
<uses-library android:name="android.test.runner" />
<activity
android:name="com.example.testdatabaseactivity.MainScreenActivity"
android:label="@string/title_activity_main_screen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.testdatabaseactivity.SearchByName"
android:label="@string/title_activity_search_by_name"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity android:name="com.dropbox.sync.android.DbxAuthActivity" />
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:launchMode="singleTask" >
<intent-filter>
<data android:scheme="db-KEY_HERE" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service
android:name="com.dropbox.sync.android.DbxSyncService"
android:enabled="true"
android:exported="false"
android:label="Dropbox Sync" />
</application>
</manifest>
编辑:没有错误,应用程序显示身份验证屏幕,我单击“允许”,然后什么也没发生。我可以按“允许”10次,但什么也没有发生。在
getFromDbx
中,您可以调用startLink
,这将打开用户界面来链接帐户。成功后,将执行您的onActivityResult
,在这里调用getFromDbx
,再次调用startLink
。:-)对我来说,这似乎是一个无限循环。恐怕这里没有什么可谈的。你能分享一下到目前为止你写的代码以及哪里出了问题吗?@smarx done,希望能帮助你:)你说你得到了一个java.lang.NullPointerException
。在你的代码中,你从哪里得到这个异常?那是错误的,我误读了我的日志,没有错误,我已经相应地编辑了这篇文章。我很抱歉。谢谢你的回复。这很有道理。我从教程示例中获取了这段代码,这也是发生在那里的情况。我会调查一下,在这段时间里,我能对代码做些什么修改来让它工作吗?Thank.smarx-因此,我将startLink调用从getFromDbx移到oncreate,现在屏幕不显示,但在onActivityResult中,它转到“else”并打印出指向Dropbox的链接失败或被取消……您能告诉我您遵循的教程是什么吗?如果它有一个bug,我想确保修复它。你能更新问题中的代码以匹配你现在所做的吗?smarx-谢谢你的帮助。我按照你告诉我的做了修复,然后我错过了一件重要的事情:获取文件信息,显然,在dropbox api中从文件获取字符串之前,你需要这样做;非常感谢:)