tutti.Autenticazione asincrona dell'account con Volley
Sto implementando un account authenticator utilizzando AbstractAccountAuthenticator e ho bisogno di chiamare un metodo asincrono nella funzione getAuthToken, per autenticare un utente. Il mio codice è simile a questo:
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;
}
...
}
Secondo la documentazione di Google per il metodo 'getAuthToken': si restituisce un risultato Bundle o null se il risultato deve essere restituito tramite la risposta. Il risultato sarà contenere alternativamente:
• AccountManager.KEY_INTENT
, o
• AccountManager.KEY_ACCOUNT_NAME
, AccountManager.KEY_ACCOUNT_TYPE
e AccountManager.KEY_AUTHTOKEN
, o
• AccountManager.KEY_ERROR_CODE
e AccountManager.KEY_ERROR_MESSAGE
per indicare un errore
e ho bisogno di restituire null perché il metodo di autenticazione è asincrono, ma come restituisco il pacchetto tramite il parametro 'response', secondo la documentazione?
Grazie a tutti, e scusa il mio inglese.
grazie, ma restituire null e OnResponse eseguire su thread diversi !!!! – SanatiSharif
@SanatiSharif Nessun problema! L'argomento 'risposta' è il tipo da te restituirai il tuo responso. È usato davvero per questo, quando il tuo metodo non può tornare immediatamente. Ad esempio, vedi le linee: 'response.onResult (result)'; e 'response.onError (result);' – Luciano