1

这是我的代码。我只是想创建一个空白文本文件并将其上传到 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 上创建了应用程序文件夹,但其中没有任何文件。我还收到以下错误跟踪:

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(通过浏览器身份验证)之前发生,所以我不确定这是原因,还是只是一个警告。有谁知道怎么了?

4

1 回答 1

1

startAuthentication调用后处理文件的代码移至onResume获取身份验证令牌的事件处理程序。

于 2013-05-11T23:07:32.643 回答