0

我有一個.net後端Azure移動服務。我一直試圖從Android客戶端獲取當前用戶信息。我正在使用以下代碼Azure移動服務getCurrentUser

private void authenticate() { 

    LoginRequest login = new LoginRequest(); 
    login.setUsername(txtUsername.getText().toString()); 
    login.setPassword(txtPassword.getText().toString()); 
    ServiceConstant.mClient.invokeApi("CustomLogin", login, LoginRequest.class, 
      new ApiOperationCallback<LoginRequest>() { 
       @Override 
       public void onCompleted(LoginRequest result, 
         Exception exception, ServiceFilterResponse response) { 
        if (exception == null) { 
         try 
         { 
          if (chkRemember.isChecked()) 
          { 
           cacheUserToken(ServiceConstant.mClient.getCurrentUser()); 
           Toast.makeText(getApplicationContext(),"cache success",Toast.LENGTH_SHORT).show(); 
          } 
         } 
         catch (Exception e) 
         { 
          Log.e(TAG, "cache fail"); 
          Toast.makeText(getApplicationContext(), 
            "cache fail", 
            Toast.LENGTH_SHORT).show(); 
         } 
         Log.i(TAG, "giris basarili " + result); 

         Intent intent = new Intent(LoginActivity.this, DeviceScanActivity.class); 
         startActivity(intent); 

        } else { 
         Log.e(TAG, "login fail" + exception); 
         alert("login", "success\n" + exception); 
        } 
       } 
      }); 
} 

當我嘗試獲取當前用戶時,它將返回null。我應該如何獲取當前用戶信息。我是Azure移動服務的新手,我沒有任何想法來解決這個問題。

+0

您是否設置了身份驗證? https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-android-get-started-users/ –

+0

是的,我已經加了認證碼 –

回答

1

這個自定義登錄代碼是基於一個定製的API,所以它的結果沒有得到當你的工作就像自動存儲到內置登錄功能。

您需要手動設置移動客戶端的CurrentUser場時,你從你的API的結果。

如果你是以下this custom auth tutorial,返回類型將實際匹配客戶的MobileServiceUser類型,並且可以設置值。有關更多詳細信息,請參閱該教程的最後一節,標題爲「使用來自客戶端的自定義身份驗證登錄」。

+0

首先感謝您爲了回答,我正在關注這個問題,並且這個過程如下http://imgur.com/Niq00pO在此之後,當我嘗試緩存用戶mAuthenticationToken的值看起來像這樣是null http://imgur.com/mpPGiTi,這是一個問題?我現在可以做些什麼,你給我點意見 –

+0

看起來類型沒有完全對齊。最簡單的解決方案可能是將響應視爲簡單的JSON對象,並提取userID和authenticationToken值。您可以將它們設置在新的MobileServiceUser上,然後將MobileServiceClient.CurrentUser分配給它。 – mattchenderson

+0

如何從response.getContent()中獲取這些值? –