Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
238 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...