无法上传.txt文件到DropBox的上传、文件、txt、DropBox

2023-09-09 21:14:45 作者:枪杀我@

下面是我的code。我只是想创建一个空白的文本文件,并将其上传到DropBox的:

Here is my code. I'm just trying to create a blank text file and upload it to 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,但没有在任何文件中。我也得到了以下错误跟踪:

When this runs, the app successfully creates the app folder on my DropBox, but without any file in it. I also get the following error trace:

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的,所以我不知道它的原因,或者只是一个警告。任何人都知道什么是错的?

This error trace seems to occur before I even have time to allow my app to access Dropbox (through the browser authentication), so I'm not sure if it's the cause, or just a warning. Anyone know what's wrong?

推荐答案

startAuthentication 将code,它与文件处理调用到 onResume 你在哪里得到的认证令牌的事件处理程序。

Move the code that deals with the file after startAuthentication call to the onResume event handler where you get the authentication tokens.