2016-11-20 33 views
0

我正在調用Firebase令牌,然後使用該令牌從服務器獲取令牌。從覆蓋回調中返回一個值

我想userSignIn()從我的服務器返回令牌。
有誰知道我該如何將代幣退回userSignIn()

@Override 
public String userSignIn(String email, String password, String authType) throws Exception { 
    login(email, password, authType, new OnLoginResponseCallback() { 
     @Override 
     public String onLoginResponse(boolean success, String token) { 
      **return token;** // how do I return this to userSignIn??? 
     } 
    }); 
} 

public interface OnLoginResponseCallback { 
    public String onLoginResponse(boolean success, String token); 
} 

public void login(String email, String password, String authType, final OnLoginResponseCallback callback) throws Exception { 
    getFirebaseToken(email, password, new OnFirebaseTokenResponseCallback() { 
     @Override 
     public String onFirebaseTokenResponse(boolean success, String token) { 
      getAuthToken(token, null, new OnAuthTokenResponseCallback(){ 
       @Override 
       public String onAuthTokenResponse(boolean success, JSONObject response){ 
        try { 
         String access_token = response.getString("access_token"); 
         callback.onLoginResponse(true, access_token); 
        } 
        catch (JSONException ex) { 

        } 
       } 
      }); 
     } 
    }); 
} 

public interface OnFirebaseTokenResponseCallback { 
    public String onFirebaseTokenResponse(boolean success, String token); 
} 

public void getFirebaseToken(String email, String password, final OnFirebaseTokenResponseCallback callback) { 
    FirebaseAuth auth = FirebaseAuth.getInstance(); 
    auth.signInWithEmailAndPassword(email, password) 
      .addOnCompleteListener(new OnCompleteListener<AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        if (!task.isSuccessful()) { 

        } else { 
         AuthResult result = task.getResult(); 
         FirebaseUser user = result.getUser(); 
         user.getToken(false).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() { 
          @Override 
          public void onComplete(@NonNull Task<GetTokenResult> task) { 
           if (task.isSuccessful()) { 
            try { 
             String token = task.getResult().getToken(); 
             callback.onFirebaseTokenResponse(true, token); 
            } 
            catch (Exception ex) { 

            } 
           } else { 

           } 
          } 
         }); 
        } 
       } 
      }); 
} 


public interface OnAuthTokenResponseCallback { 
    public String onAuthTokenResponse(boolean success, JSONObject response); 
} 

public void getAuthToken(String token, String refreshToken, final OnAuthTokenResponseCallback callback) throws JSONException { 
    RequestParams params = new RequestParams(); 
    if (refreshToken != null) 
    { 
     params.add("grant_type", "refresh_token"); 
     params.add("refresh_token", refreshToken); 
    } 
    else if (token != null) 
    { 
     params.add("grant_type", "urn:ietf:params:oauth:grant-type:firebase_token"); 
     params.add("assertion", token); 
    } 
    else if (refreshToken == null && token == null) 
    { 
     params.add("grant_type", "password"); 
     params.add("username", ""); 
     params.add("password", ""); 
    } 
    AuthClient.post("connect/token", params, new JsonHttpResponseHandler() { 
     @Override 
     public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, JSONObject response) { 
      try { 
       callback.onAuthTokenResponse(true, response); 
      } catch (Exception ex) { 

      } 
     } 
     @Override 
     public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, Throwable throwable, JSONObject response) { 
      callback.onAuthTokenResponse(false, new JSONObject()); 
     } 
    }); 
} 

UPDATE:

感謝。我去掉了多餘的方法和呼叫登錄這樣的:

.login(userName, userPass, mAuthTokenType, new OnLoginResponseCallback() { 
    @Override 
    public void onLoginResponse(boolean success, String token) { 
    data.putString(AccountManager.KEY_ACCOUNT_NAME, userName); 
    data.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType); 
    data.putString(AccountManager.KEY_AUTHTOKEN, token); 
    data.putString(PARAM_USER_PASS, userPass); 
    } 
}); 

,我認爲它的工作原理,但還沒有來得及充分測試它。我不確定的一件事是我試圖用「token」中的值修改「data」,然而「data」是一個最終的Bundle,所以我不確定它是否有效。稍後再測試。謝謝。

+2

「任何人都知道我可以返回令牌 'userSignIn'?」 - 你沒有。從'onLoginResponse()'方法啓動任何需要'token'的代碼。 –

+1

你的接口方法不需要返回數據。他們都應該是無效的 –

回答

1

你們稱是基本要求相同簽名

@Override 
public String userSignIn(String email, String password, String authType) throws Exception { 
    login(email, password, authType, new OnLoginResponseCallback() { 
     @Override 

的另一種方法,而不是一種方法,無論你會打電話userSignIn,你叫login,並傳入匿名類。你不能從這些內部方法返回,因爲這不是回調函數的工作方式。你使用接口方法的參數來「繼續」你的邏輯。比如,使用某些用戶信息進行登錄,回調主函數,使用此信息發出新請求,等待該數據的回調,將數據傳回給其他方法。這全是void方法調用其他方法。沒有return陳述

雖然,在Javascript中,你可以看到這篇

How to return value from an asynchronous callback function?

0

爲什麼你想從那裏返回令牌,這不是很清楚,因爲你已經在該方法下了!你可以在onLoginResponse()之內完成其餘的工作,或者通過調用另一種方法。