无法在android中的dropbox上传SQlite数据库文件

时间:2014-07-12 05:56:19

标签: android sqlite dropbox dropbox-api

我想将我的SQlite数据库文件上传到Dropbox下面是我的代码

public class DropboxActivity extends Activity   {

private DropboxAPI<AndroidAuthSession> dropbox;

private final static String FILE_DIR = Environment.getExternalStorageDirectory()+"/MyFolder/BD/Demo.sqlite";
private final static String DROPBOX_NAME = "dropbox_prefs";
private final static String ACCESS_KEY = "key";
private final static String ACCESS_SECRET = "sec_key";
private boolean isLoggedIn;
private Button logIn;
private Button uploadFile;
private Button listFiles;
private LinearLayout container;

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy); 
    logIn = (Button) findViewById(R.id.dropbox_login);
    uploadFile = (Button) findViewById(R.id.upload_file);
    listFiles = (Button) findViewById(R.id.list_files);
    container = (LinearLayout) findViewById(R.id.container_files);

    uploadFile.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("UPload");
            UploadFileToDropbox upload = new UploadFileToDropbox(DropboxActivity.this, dropbox, FILE_DIR);
            upload.execute();
        }
    });

    logIn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (isLoggedIn) {
                dropbox.getSession().unlink();
                loggedIn(false);
            } else {
                dropbox.getSession().startAuthentication(DropboxActivity.this);
            }
        }
    });
    listFiles.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ListDropboxFiles list = new ListDropboxFiles(dropbox, FILE_DIR,
                    handler);
            list.execute();
        }
    });
    loggedIn(false);

    AndroidAuthSession session;
    AppKeyPair pair = new AppKeyPair(ACCESS_KEY, ACCESS_SECRET);

    SharedPreferences prefs = getSharedPreferences(DROPBOX_NAME, 0);
    String key = prefs.getString(ACCESS_KEY, null);
    String secret = prefs.getString(ACCESS_SECRET, null);

    if (key != null && secret != null) {
        AccessTokenPair token = new AccessTokenPair(key, secret);
        session = new AndroidAuthSession(pair, AccessType.APP_FOLDER, token);
    } else {
        session = new AndroidAuthSession(pair, AccessType.APP_FOLDER);
    }

    dropbox = new DropboxAPI<AndroidAuthSession>(session);
}

@Override
protected void onResume() {
    super.onResume();

    AndroidAuthSession session = dropbox.getSession();
    if (session.authenticationSuccessful()) {
        try {
            session.finishAuthentication();

            TokenPair tokens = session.getAccessTokenPair();
            SharedPreferences prefs = getSharedPreferences(DROPBOX_NAME, 0);
            Editor editor = prefs.edit();
            editor.putString(ACCESS_KEY, tokens.key);
            editor.putString(ACCESS_SECRET, tokens.secret);
            editor.commit();

            loggedIn(true);
        } catch (IllegalStateException e) {
            Toast.makeText(this, "Error during Dropbox authentication",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

public void loggedIn(boolean isLogged) {
    isLoggedIn = isLogged;
    uploadFile.setEnabled(isLogged);
    listFiles.setEnabled(isLogged);
    logIn.setText(isLogged ? "Log out" : "Log in");
}

private final Handler handler = new Handler() {
    public void handleMessage(Message msg) {

        ArrayList<String> result = msg.getData().getStringArrayList("data");

        for (String fileName : result) {
            Log.i("ListFiles", fileName);

            TextView tv = new TextView(DropboxActivity.this);
            tv.setText(fileName);

            container.addView(tv);
        }
    }
};

}

UploadFileToDropbox.java

public class UploadFileToDropbox extends AsyncTask<Void, Void, Boolean> {

private DropboxAPI<?> dropbox;
private String path;
private Context context;

public UploadFileToDropbox(Context context, DropboxAPI<?> dropbox,
        String path) {
    this.context = context.getApplicationContext();
    this.dropbox = dropbox;
    this.path = path;
}

@Override
protected Boolean doInBackground(Void... params) {
    final File tempDir = context.getCacheDir();
    File tempFile;
    FileWriter fr;
    try {

        tempFile=new File(path);
        System.out.println("path "+path);
        FileInputStream fileInputStream = new FileInputStream(tempFile);
        dropbox.putFile(path, fileInputStream,tempFile.length(), null, null);
        tempFile.delete();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DropboxException e) {
        e.printStackTrace();
    }

    return false;
}

@Override
protected void onPostExecute(Boolean result) {
    if (result) {
        Toast.makeText(context, "File Uploaded Sucesfully!",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(context, "Failed to upload file", Toast.LENGTH_LONG)
                .show();
    }
}

}

当我在我的logcat上面运行时,我的logcat

给出了如下错误
DropboxServerException (nginx): 403 None (Forbidden)
at com.dropbox.client2.RESTUtility.parseAsJSON(RESTUtility.java:265)
at com.dropbox.client2.RESTUtility.execute(RESTUtility.java:415)
at com.dropbox.client2.DropboxAPI$BasicUploadRequest.upload(DropboxAPI.java:1119)
at com.dropbox.client2.DropboxAPI.putFile(DropboxAPI.java:1460)
at com.example.dropdemo.UploadFileToDropbox.doInBackground(UploadFileToDropbox.java:42)
at com.example.dropdemo.UploadFileToDropbox.doInBackground(UploadFileToDropbox.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Removing a connection that never existed!

在非活动的InputConnection上的getSelectedText    setComposingText在非活动的InputConnection

以上错误在下面给出了

dropbox.putFile(path,fileInputStream,tempFile.length(),null,null);

任何想法我怎么能解决这个问题?

0 个答案:

没有答案