com.dropbox.client2.exception.DropboxUnlinkedException [英] com.dropbox.client2.exception.DropboxUnlinkedException

查看:275
本文介绍了com.dropbox.client2.exception.DropboxUnlinkedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的文本文件发送到Dropbox的,但它显示 DropboxUnlinkedException

I want to send text file to dropbox but it is showing DropboxUnlinkedException.

解决方案:

首先,让你的程序得到充分验证。刚过 mDBApi.getSession.startAuthentication()方法,onResume方法会自动调用。让完全认证得到完成,然后做你想做的事。

First, let your program get fully authenticated. Just after mDBApi.getSession.startAuthentication() method, onResume method will get called automatically. Let the full authentication get completed and then do what do you want to do.

MainActivity

public class MainActivity extends Activity implements LocationListener{

TextView date;
TextView lati;
TextView longi;
Button b1;
private DropboxAPI<AndroidAuthSession> mDBApi;
private LocationManager locationManager;
private String provider;

final static public String ACCOUNT_PREFS_NAME = "GPS_File";
final static public String APP_KEY = "5qiq4z06ikagxfb";
final static public String APP_SECRET = "f6mbf1hnn0re2ni";
final static public AccessType ACCESS_TYPE = AccessType.APP_FOLDER;

boolean mIsLoggedIn = false;

@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);

 //this is start authentication     
   mDBApi.getSession().startAuthentication(MainActivity.this);
 //after this it will call onResume 

    date = (TextView)findViewById(R.id.textView2);
    lati = (TextView)findViewById(R.id.textView4);
    longi = (TextView)findViewById(R.id.textView6);
    b1 = (Button)findViewById(R.id.button1);

    createFile("abcd", "12345", "54321");
    toDropbox();

    setLoggedIn(mDBApi.getSession().isLinked());
}

void createFile(String str1,String str2,String str3)
{
    String data = str1+"\t"+str2+"\t"+str3;
    try{
        File myFile = new File("/sdcard/DropboxFile1.txt");
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
      myOutWriter.append(data);
      myOutWriter.close();
      fOut.close();
        }
        catch(Exception e)
        {e.printStackTrace();}
}


void toDropbox()
{
    b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String filePath = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/DropboxFile1.txt";
            File file = new File(filePath);

            mDBApi.getSession().startAuthentication(MainActivity.this);

            try {

                mDBApi.putFileOverwrite(filePath, new FileInputStream(file), file.length(), null);

            } catch (DropboxException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    });
}

private void clearKeys() {
    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
    Editor edit = prefs.edit();
    edit.clear();
    edit.commit();
}

//This get call after StartAuthentication..
protected void onResume() {
    super.onResume();
    AndroidAuthSession session = mDBApi.getSession();
    // The next part must be inserted in the onResume() method of the
    // activity from which session.startAuthentication() was called, so
    // that Dropbox authentication completes properly.
    if (session.authenticationSuccessful()) {
        try {
            // Mandatory call to complete the auth
            session.finishAuthentication();

            // Store it locally in our app for later use
            TokenPair tokens = session.getAccessTokenPair();
            storeKeys(tokens.key, tokens.secret);
            setLoggedIn(true);
        } catch (IllegalStateException e) {
//Keep this toast.. It will show you the completed authentication..
            Toast.makeText(getBaseContext(), e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            Log.i("Dropbox", "Error authenticating", e);
        }
    }
}    

private void storeKeys(String key, String secret) {
    // Save the access key for later
    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
    Editor edit = prefs.edit();
    edit.putString(APP_KEY, key);
    edit.putString(APP_SECRET, secret);
    edit.commit();
}
public void setLoggedIn(boolean loggedIn) {
    mIsLoggedIn = loggedIn;
}
public boolean isLoggedIn() {
    return mIsLoggedIn;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub
}
}//MainActivity Ends..`

`
获得认证完成后,做你的另一东西。

` After authentication get completed do your another stuff.

推荐答案

您正在呼叫 startAuthentication 但随后立即试图调用API方法(前验证实际发生的情况) 。您只能使用API​​一旦用户进行身份验证。在您的code,这里的运行用户身份验证和之后的部分返回到您的应用程序:

You're calling startAuthentication but then immediately trying to call API methods (before authentication has actually happened). You can only use the API once the user has authenticated. In your code, here's the part that runs after the user has authenticated and returns to your app:

protected void onResume() {
    ...
    if (session.authenticationSuccessful()) {
        ...

这篇关于com.dropbox.client2.exception.DropboxUnlinkedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆