everyone。Volley的異步賬號認證
我正在使用AbstractAccountAuthenticator實現帳戶身份驗證器,並且我需要在函數getAuthToken中調用異步方法來驗證用戶身份。 我的代碼是這樣的:
public class AccountAuthenticator extends AbstractAccountAuthenticator {
...
@Override
public Bundle getAuthToken(final AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)
throws NetworkErrorException
{
final AccountManager accountManager = AccountManager.get(context);
String authToken = accountManager.peekAuthToken(account, authTokenType);
// !!!!
if(TextUtils.isEmpty(authToken)) {
<<call asynchronous method to acquire token>>
return null;
}
// !!!!
final Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
return result;
}
...
}
根據谷歌的文檔,以「getAuthToken」的方法: 它返回一個Bundle結果或NULL,如果結果是通過響應歸還。 結果將包含兩種:0 •AccountManager.KEY_INTENT
,或
•AccountManager.KEY_ACCOUNT_NAME
,AccountManager.KEY_ACCOUNT_TYPE
和AccountManager.KEY_AUTHTOKEN
,或
•AccountManager.KEY_ERROR_CODE
和AccountManager.KEY_ERROR_MESSAGE
指示錯誤
,我需要返回null,因爲認證方法是異步的,但根據文檔,我是如何通過'response'參數返回Bundle的?
謝謝大家,對不起我的英語。
謝謝,但返回null和OnResponse運行在不同的線程!!!! – SanatiSharif 2015-02-28 20:24:14
@SanatiSharif沒問題! 「迴應」這個論點是你的傢伙會退回你的respost。當你的方法不能立即返回的時候,它真的用於這個。例如,請參閱行:'response.onResult(result)';和'response.onError(result);' – Luciano 2015-10-19 18:12:44