0

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());
        } 
    }
}

I am also posting the error log here.

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
  • 1
    Dropbox is denying you access, but your code looks OK. Have you followed their setup procedures correctly? Commented Nov 21, 2013 at 14:53
  • Yes everything. unfortunately its not working Commented Nov 21, 2013 at 17:00

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What does "not working" mean? Please add the new errors to the question.
I have added the new logs, there is no error coming but a few warnings which are causing it to stop the uploading process
1

The problem is the lines

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

You should fill in the values provided by Dropbox.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.