AWS AppSync错误-空对象引用上的AWSMobileClientStore.get(String)

时间:2019-05-13 09:11:38

标签: android amazon-web-services aws-sdk aws-appsync appsync-apollo-client

APPSYNC_ERROR: Attempt to invoke virtual method
  

java.util.Map   com.amazonaws.mobile.client.AWSMobileClientStore.get(java.lang.String [])    在空对象引用上

我正在尝试在AWS表中插入数据,但出现此错误。 您能帮我解决这个问题吗?

AWSAppSyncClient client = AWSAppSyncClient.builder()
                .context(context)
                .region(AppHelper.cognitoRegion)
                .serverUrl(AppHelper.SERVER_URL)
                .cognitoUserPoolsAuthProvider(new CognitoUserPoolsAuthProvider() {
                    @Override
                    public String getLatestAuthToken() {
                        try {
                            return AWSMobileClient.getInstance().getTokens().getIdToken().getTokenString();
                        } catch (Exception e){
                            Log.e("APPSYNC_ERROR", e.getLocalizedMessage());
                            return e.getLocalizedMessage();
                        }
                    }
                }).persistentMutationsCallback(new PersistentMutationsCallback() {
                @Override
                public void onResponse(PersistentMutationsResponse response) {
                    Log.d("NOTERROR", response.getMutationClassName());
                }

                @Override
                public void onFailure(PersistentMutationsError error) {
                    Log.e("TAG", error.getMutationClassName());
                    Log.e("TAG", "Error", error.getException());
                }
                }).build();

1 个答案:

答案 0 :(得分:1)

在使用AWSMobileClient之前,您需要成功对其进行初始化。

AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {
     @Override
    public void onResult(UserStateDetails userStateDetails) {
        switch (userStateDetails.getUserState()){
            case SIGNED_IN:
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView textView = (TextView) findViewById(R.id.text);
                        textView.setText("Logged IN");
                    }
                });
                break;
            case SIGNED_OUT:
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView textView = (TextView) findViewById(R.id.text);
                        textView.setText("Logged OUT");
                    }
                });
                break;
            default:
                AWSMobileClient.getInstance().signOut();
                break;
        }
    }

    @Override
    public void onError(Exception e) {
        Log.e("INIT", e.toString());
    }
});

如果initialize成功,您将能够通过getTokens()方法检索令牌。

来源:https://aws-amplify.github.io/docs/android/authentication