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

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

本文介绍了从Android应用程序将文件上传到Dropbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to upload a text file to Dropbox. The authentication works fine, but when it tries to upload the file, the application crashes. here is my code

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!


推荐答案

您正在尝试在主机上执行网络操作线程,这是为了避免阻塞UI。有关详细信息,请参见。

You are attempting to perform a network operation on the main thread, which is forbidden to avoid blocking the UI. See the documentation of NetworkOnMainThreadException for details.

这篇关于从Android应用程序将文件上传到Dropbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:15