2016-11-20 68 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()之内完成其余的工作,或者通过调用另一种方法。