java - Android process for "silently" using refresh token to get new access token -


i trying handle situation in demo application i'm writing have expired access token, , need use refresh token new access token. background tasks this, i'd use asynctask app login (which works fine), i'm not sure how - if can done (i'm sure can, i'm not seeing right now).

below current flow - call get() blocks ui thread, isn't i'm supposed doing here. our access ticket class contains both access token , refresh token.

public boolean isloggedin() {     if (isfreshaccesstoken())         return true;      // if access token expired, we'll need use     // refresh token one.  use background task     // this.     string refreshtoken = preferences.getstring(refresh_token_key, null);     if (stringutils.isnotblank(refreshtoken)) {         refreshaccesstokenasynctask refreshlogin = new refreshaccesstokenasynctask();         refreshlogin.execute(refreshtoken);          try {             return refreshlogin.get();         } catch (exception e) {             log.d(tag, "isloggedin() : exception in async task.  returning false.");             return false;         }     } else         return false; } 

here asynctask implementation looks like. refreshaccessticket() method makes rest call accessticket response.

private class refreshaccesstokenasynctask extends asynctask<string, void, boolean> {      @override     protected boolean doinbackground(string... params) {         string refreshtoken = params[0];         try {              // new access token using refresh token             // in background ...             accessticket accessticket = refreshaccessticket(refreshtoken);              // ... , save updated information shared             // preferences             saveaccessticket(accessticket);             return true;         } catch (exception ex) {              // if call new access token fails reason,             // return false.  we'll force user log in again             // new access ticket.             return false;         }     }      @override     protected void onpostexecute(boolean isloggedin) {         super.onpostexecute(isloggedin);         log.d(tag, "refreshaccesstokenasynctask.onpostexecute() : " +                 "returning isloggedin value of " + isloggedin);     }  } 

what missing here? mentioned above, want process silent - if access token has expired, i want use refresh token automatically new access token without user having or have app jump out of current flow (such getting friends list, etc.) - happen in background. should using other asynctask?

asynctask should work fine, can't result that. try instead.

private interface callback { void done(boolean result); }  private class refreshaccesstokenasynctask extends asynctask<string, void, boolean> {  private callback cb;  private refreshaccesstokenasynctask(callback callback)     cb = callback; }  @override protected boolean doinbackground(string... params) {     string refreshtoken = params[0];     try {          // new access token using refresh token         // in background ...         accessticket accessticket = refreshaccessticket(refreshtoken);          // ... , save updated information shared         // preferences         saveaccessticket(accessticket);         return true;     } catch (exception ex) {          // if call new access token fails reason,         // return false.  we'll force user log in again         // new access ticket.         return false;     } }  @override protected void onpostexecute(boolean isloggedin) {     super.onpostexecute(isloggedin);     log.d(tag, "refreshaccesstokenasynctask.onpostexecute() : " +             "returning isloggedin value of " + isloggedin);     cb.done(isloggedin); } 

}

then use asynctask so:

public boolean isloggedin() { if (isfreshaccesstoken())     return true;  // if access token expired, we'll need use // refresh token one.  use background task // this. string refreshtoken = preferences.getstring(refresh_token_key, null); if (stringutils.isnotblank(refreshtoken)) {     callback callback = new callback() {         public void done(boolean result){             // whatever need here         }     }     refreshaccesstokenasynctask refreshlogin = new refreshaccesstokenasynctask(callback);     refreshlogin.execute(refreshtoken); } else     return false; } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -