这是我的代码。我只是想创建一个空白文本文件并将其上传到DropBox:
public class MainActivity extends Activity {
final static private String APP_KEY = "APP_KEY";
final static private String APP_SECRET = "APP_SECRET";
final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
private DropboxAPI<AndroidAuthSession> mDBApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
mDBApi.getSession().startAuthentication(MainActivity.this);
String filePath = getApplicationContext().getFilesDir().getPath().toString() + "/magnus-opus.txt";
File file = new File(filePath);
try {
file.createNewFile();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Entry response = mDBApi.putFile("/magnum-opus.txt", inputStream,
file.length(), null, null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);
} catch (DropboxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/* Called when the application resumes */
@Override
protected void onResume()
{
super.onResume();
if (mDBApi.getSession().authenticationSuccessful()) {
try {
// Required to complete auth, sets the access token on the session
mDBApi.getSession().finishAuthentication();
AccessTokenPair tokens = mDBApi.getSession().getAccessTokenPair();
} catch (IllegalStateException e) {
Log.i("DbAuthLog", "Error authenticating", e);
}
}
}
}
当它运行时,应用程序在我的DropBox上成功创建了app文件夹,但没有任何文件。我还得到以下错误跟踪:
05-11 13:06:42.911: W/System.err(10467): com.dropbox.client2.exception.DropboxUnlinkedException
05-11 13:06:42.911: W/System.err(10467): at com.dropbox.client2.DropboxAPI.assertAuthenticated(DropboxAPI.java:2486)
05-11 13:06:42.911: W/System.err(10467): at com.dropbox.client2.DropboxAPI.putFileRequest(DropboxAPI.java:2138)
05-11 13:06:42.911: W/System.err(10467): at com.dropbox.client2.DropboxAPI.putFileRequest(DropboxAPI.java:1459)
05-11 13:06:42.911: W/System.err(10467): at com.dropbox.client2.DropboxAPI.putFile(DropboxAPI.java:1419)
05-11 13:06:42.921: W/System.err(10467): at com.example.receptionlookup.MainActivity.onCreate(MainActivity.java:77)
05-11 13:06:42.921: W/System.err(10467): at android.app.Activity.performCreate(Activity.java:5066)
05-11 13:06:42.921: W/System.err(10467): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
05-11 13:06:42.921: W/System.err(10467): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
05-11 13:06:42.921: W/System.err(10467): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
05-11 13:06:42.921: W/System.err(10467): at android.app.ActivityThread.access$600(ActivityThread.java:151)
05-11 13:06:42.921: W/System.err(10467): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
05-11 13:06:42.921: W/System.err(10467): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 13:06:42.921: W/System.err(10467): at android.os.Looper.loop(Looper.java:155)
05-11 13:06:42.921: W/System.err(10467): at android.app.ActivityThread.main(ActivityThread.java:5454)
05-11 13:06:42.921: W/System.err(10467): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 13:06:42.921: W/System.err(10467): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 13:06:42.921: W/System.err(10467): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
05-11 13:06:42.921: W/System.err(10467): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
05-11 13:06:42.921: W/System.err(10467): at dalvik.system.NativeStart.main(Native Method)
这个错误跟踪似乎发生在我甚至没有时间允许我的应用程序访问Dropbox(通过浏览器身份验证)之前,所以我不确定它是原因,还是仅仅是一个警告。谁知道什么是错的?
答案 0 :(得分:1)
在startAuthentication
调用onResume
事件处理程序后,将处理该文件的代码移动到您获取身份验证令牌的位置。