从Android应用程序上传文件到Dropbox

时间:2013-11-21 09:34:24

标签: android dropbox-api

我正在尝试将文本文件上传到Dropbox。身份验证工作正常,但当它尝试上载文件时,应用程序崩溃。这是我的代码

public class Dropboxupload extends Activity {
    final static private String APP_KEY = "KEY";
    final static private String APP_SECRET = "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(Dropboxupload.this);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.dropboxupload, 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);
            }

            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 {
                com.dropbox.client2.DropboxAPI.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();
            }

        }
    }


private void writeToFile(String data,String filepath, String filename) {
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(filepath, Context.MODE_PRIVATE));
            outputStreamWriter.write(data);
            outputStreamWriter.close();
        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        } 
    }
}

我也在这里发布错误日志。

11-21 18:04:20.094: W/System.err(16838): DropboxServerException (nginx): 403 Forbidden (Forbidden)
11-21 18:04:20.094: W/System.err(16838):    at com.dropbox.client2.RESTUtility.parseAsJSON(RESTUtility.java:263)
11-21 18:04:20.094: W/System.err(16838):    at com.dropbox.client2.RESTUtility.execute(RESTUtility.java:411)
11-21 18:04:20.094: W/System.err(16838):    at com.dropbox.client2.DropboxAPI$BasicUploadRequest.upload(DropboxAPI.java:1080)
11-21 18:04:20.104: W/System.err(16838):    at com.dropbox.client2.DropboxAPI.putFile(DropboxAPI.java:1421)
11-21 18:04:20.104: W/System.err(16838):    at com.example.screenwritter.Dropboxupload$1.run(Dropboxupload.java:85)
11-21 18:04:20.104: W/System.err(16838):    at java.lang.Thread.run(Thread.java:856)
11-21 18:04:43.506: W/IdleConnectionHandler(16838): Removing a connection that never existed!

2 个答案:

答案 0 :(得分:2)

您正在尝试在主线程上执行网络操作,禁止阻止UI。有关详细信息,请参阅the documentation of NetworkOnMainThreadException

答案 1 :(得分:1)

问题是线

final static private String APP_KEY = "KEY";
final static private String APP_SECRET = "SECRET";

您应该填写Dropbox提供的值。