使用Dropbox REST通过Android同步你的列表

时间:2012-01-16 12:47:26

标签: android rest dropbox

我希望我的列表与dropbox REST api同步。                我怎样才能做到这一点。        点击按钮我正在上传内容。                 但我得到了DropboxUnlinkedException

这是我的代码

 mSubmit = (Button)findViewById(R.id.auth_button);

 mSubmit.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
 // This logs you out if you're logged in, or vice versa
// Uploading content.

 AppKeyPair    appKeys =     new     AppKeyPair(APP_KEY, APP_SECRET);
 AndroidAuthSession     session     =     buildSession();
 mApi = new DropboxAPI<AndroidAuthSession>(session);

 String fileContents = listItems.toString();

 ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContents.getBytes());
 try {

 Entry newEntry = mApi.putFile("/testing.txt", inputStream,
fileContents.length(), null, null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + newEntry.rev);
 } catch (DropboxUnlinkedException e) {
// User has unlinked, ask them to link again here.
 Log.e("DbExampleLog", "User has unlinked.");
 } catch (DropboxException e) {
 Log.e("DbExampleLog", "Something went wrong while uploading.");
 }
}
});


   private String[] getKeys() {
   // TODO Auto-generated method stub
      SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
      String key = prefs.getString(ACCESS_KEY_NAME, null);
      String secret = prefs.getString(ACCESS_SECRET_NAME, null);
      if (key != null && secret != null) {
      String[] ret = new String[2];
      ret[0] = key;
      ret[1] = secret;
      return ret;
      } else {
      return null;
      }

     }



   private AndroidAuthSession buildSession() {

        AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
       AndroidAuthSession session;
       String[] stored = getKeys();
        if (stored != null) {
        AccessTokenPair accessToken = new AccessTokenPair(stored[0], stored[1]);
        session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE, accessToken);
        } else {
        session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
      }
       return session;
        }

我不知道我的代码有什么问题。        任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

//结帐这个。这对你有用吗<​​/ p>

AndroidAuthSession     session     =     buildSession();
 mApi = new DropboxAPI<AndroidAuthSession>(session);
        //setLoggedIn(mApi.getSession().isLinked());// I am checking here for its logged in or not

        try {
            if(session.getAccessTokenPair() !=null){
                Log.e("hi","authenticationSuccessful"); 
                //setLoggedIn(true);

    Entry dirent1 = mApi.metadata("/", 1000, null, true, null);
            }
        } catch (DropboxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

答案 1 :(得分:0)

在使用之前

mApi.putFile("/testing.txt", inputStream,
    fileContents.length(), null, null)

您必须首先进行身份验证,如下所示,它为mApi配备了访问令牌和访问密钥。

bAuth.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View v) {
        // This logs you out if you're logged in, or vice versa
        if (mLoggedIn) {
            logOut();
        } else {
            // Start the remote authentication
            mApi.getSession().startAuthentication(MCActivity.this);//this directs you to your browser for authentication
        }
    }
});

在此之后你可以自由使用mApi ..

相关问题