2014-01-10 47 views
1

我有問題讓用戶登錄我所有的應用程序生活,「登錄」是在主要活動,當開始另一個活動,我仍然在裏面,我可以上傳數據到quickblox,但後我開始另一個活動,然後重新開始,我上傳的數據quickblox,我得到的錯誤時,試圖上傳數據activy:「令牌需要」 ......如何讓用戶登錄應用程序的生活?

編輯:

QBSettings.getInstance().fastConfigInit(String.valueOf(APP_ID), AUTH_KEY, AUTH_SECRET); 
    QBUser user = new QBUser("login", "password"); 
    QBAuth.createSession(user, this, QBQueries.SIGN_IN); 
+2

不應該有更多的代碼?但是從你的問題來看,我相信你需要存儲用戶的令牌,並將它傳遞給每一個API請求(這是大多數API的工作原理)。 – raulriera

+1

你發佈的代碼不能不那麼相關。請發佈處理令牌和API請求的代碼。 –

+0

如果我已經在唱歌我不需要「傳遞每一個API請求」 – Cohelad

回答

1

我認爲這是另外一個問題

「令牌需要」的意思是你沒有創建會話,並試圖執行其他查詢

您必須先正確創建會話

 QBAuth.createSession(new QBCallbackImpl() { 
      @Override 
      public void onComplete(Result result) { 
       if (result.isSuccess()) { 
        // do other requests here 
        // 
       } else { 
        handleErrors(result); 
       } 
      } 
     }); 

如果這不是你的問題 - 請你的問題提供更多的代碼

UPD

1)請檢查令牌爲空

try { 
    String token = BaseService.getBaseService().getToken(); 
    if(token == null){ 
     // recreate session here 
    } 
} catch (BaseServiceException e) { 
    e.printStackTrace(); 
} 
+0

謝謝,這不是我的問題,我沒有問題登錄,它的工作很好,我可以上傳和下載數據沒有任何問題,我的問題是,在我的應用程序一段時間後(從活動轉移到活動所以..)隨機它停止工作 - 就像它正在執行'註銷'它自我 – Cohelad

+1

嗯,看起來像令牌變爲空。 嘗試記錄每次一個令牌BaseService.getBaseService()。getToken() 並檢查,如果它由於某種原因爲空 - 重新創建一個會話 –

+0

謝謝,我會試試這個.. Igor,你是最棒的!繼續.. – Cohelad

0

林不知道如何使用這裏使用的API,但如果你想要一個備用的建議,我總是使用SharedPreferences來保存用戶會話。

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
SharedPreferences.Editor editor = settings.edit(); 
editor.putString("username", username); // Save the username with tag "username" 
editor.putString("password", password); // Save the password with tag "password" 
editor.commit(); 

而獲得用戶信息回:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
u = settings.getString("username", null); 
p = settings.getString("password", null); 
if(u == null && p == null) {...} // No saved user session, have user sign in 
else {...} // User already logged in, go to main screen 
+0

謝謝,這不是我的問題,我在SharedPreferences上保存用戶的詳細信息,但在這個Api中,你只需要簽署在Api請求中,您不需要再次簽名,也不需要輸入用戶詳細信息。 – Cohelad

相關問題