2

显示保管箱文件列表时遇到问题。当我从 dropbox 成功验证并获得访问令牌时,我设置了访问令牌mDBApi.getSession().setAccessTokenPair(tokens); ,然后它将停止在Entry contact = mDBApi.metadata("/", 1000, null, true, null);我调查关于 dorpbox 上显示文件列表的大量解决方案时,我无法找出与我的代码的区别。请帮我 :(

public class DropBoxActivity extends Activity{
final static private String APP_KEY = "MyKey";
final static private String APP_SECRET = "MySecret";
final static private AccessType ACCESS_TYPE = AccessType.DROPBOX;
// In the class declaration section:
private DropboxAPI<AndroidAuthSession> mDBApi;
SharedPreferences sharedPref;


@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);        

    setContentView(R.layout.activity_drop_box); 
    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

    // And later in some initialization function:
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    mDBApi.getSession().startAuthentication(this);
    Log.d("DbAuthLog", "OnCreateEnd");

}


@Override
protected void onResume(){
    super.onResume();
    AccessTokenPair tokens=null;  
    //return control
     if (mDBApi != null && mDBApi.getSession().authenticationSuccessful()) {
         try{
               //MANDATORY call to complete auth.
               //Sets the access token on the session
             mDBApi.getSession().finishAuthentication();

             tokens = mDBApi.getSession().getAccessTokenPair();              

             // Provide your own storeKeys to persist the access token pair
             // A typical way to store tokens is using SharedPreferences
             storeKeys(tokens.key, tokens.secret);


         }catch(IllegalStateException e){

             Log.i("DbAuthLog", "Error authenticating", e);
         } 

     }

     try {           

        Log.d("DbAuthLog", "step0");
        AccessTokenPair access = getStoredKeys();
        mDBApi.getSession().setAccessTokenPair(tokens);
        Entry contact = mDBApi.metadata("/", 1000, null, true, null);
        Log.d("DbAuthLog", "step1");
        List<Entry> CFolder = contact.contents;
        Log.d("DbAuthLog", "step2");
          for (Entry entry : CFolder) {
                Log.i("DbAuthLog", "Filename: " + entry.fileName());
          }
          Log.d("DbAuthLog", "OnResumeEnd");

    } catch(DropboxServerException e){
        Log.d("DbAuthLog", "DropboxServerException");
        e.printStackTrace();
    } catch(DropboxUnlinkedException e){
        Log.d("DbAuthLog", "DropboxUnlinkedException");
        e.printStackTrace();
    } catch (DropboxIOException e) {
        // TODO Auto-generated catch block
        Log.d("DbAuthLog", "DropboxIOException");
        e.printStackTrace();
    } catch (DropboxException e) {
        // TODO Auto-generated catch block
        Log.d("DbAuthLog", "DropboxException");
        e.printStackTrace();
    } catch (NullPointerException e){
        Log.d("DbAuthLog", "DropboxException");
    }

}

private void storeKeys(String key, String secret){

    Log.d("DbAuthLog", "storeKeys");
    sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("key", key);
    editor.putString("secret", secret);
    editor.commit();
    Log.d("DbAuthLog", key);
    Log.d("DbAuthLog", secret);


}
4

0 回答 0