使用Neura构建Android项目时收到错误

时间:2016-07-24 14:56:08

标签: android neura

我正在使用neura sdk来检测用户的当前数据(他/她在哪里,10分钟前他们在哪里等)。

我想登录他们的api,然后验证我的用户,但是当我打电话给NeuraApiClient.authenticate时(...)没有任何反应。

我跟着neura documentations,但仍然 - 没有任何反应。

这是我的代码:

public class MainActivity extends AppCompatActivity {

    private ArrayList<Permission> mPermissions;
    private AuthenticationRequest mAuthenticateRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Builder builder = new Builder(this);  
        NeuraApiClient neuraApiClient = builder.build();     
        neuraApiClient.setAppUid(getResources().getString(R.string.app_uid));        
        neuraApiClient.setAppSecret(getResources().getString(R.string.app_secret));  
        neuraApiClient.connect();    

        fetchPermissions(neuraApiClient);
        neuraApiClient.authenticate(100, mAuthenticateRequest);
    }

    private void fetchPermissions(final NeuraApiClient client) {
        client.getAppPermissions(new GetPermissionsRequestCallbacks() {
            @Override
            public void onSuccess(final List<Permission> permissions) throws RemoteException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mPermissions = new ArrayList<>(permissions);
                        mAuthenticateRequest = new AuthenticationRequest();
                        mAuthenticateRequest.setAppId(client.getAppUid());
                        mAuthenticateRequest.setAppSecret(client.getAppSecret());
                        mAuthenticateRequest.setPermissions(mPermissions);
                    }
                });
            }

            @Override
            public void onFailure(Bundle resultData, int errorCode) throws RemoteException {
            }

            @Override
            public IBinder asBinder() {
                return null;
            }
        });
    }
} 

1 个答案:

答案 0 :(得分:2)

getAppPermissions是一个异步调用,数据是在GetPermissionsRequestCallbacks上获取的。 在GetPermissionsRequestCallbacks中,您正在启动正在使用身份验证方法的mAuthenticateRequest。 这意味着您必须等待,直到调用GetPermissionsRequestCallbacks的onSuccess,然后才能调用

neuraApiClient.authenticate(100, mAuthenticateRequest);

基本上,如果您不等待获取mAuthenticateRequest,则使用mAuthenticateRequest = null进行身份验证,并且neuraApiClient.authenticate(..)将失败。

您可以执行以下操作:在收到结果时调用身份验证 -

private void fetchPermissions(final NeuraApiClient client) {
        client.getAppPermissions(new GetPermissionsRequestCallbacks() {
            @Override
            public void onSuccess(final List<Permission> permissions) throws RemoteException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mPermissions = new ArrayList<>(permissions);
                        mAuthenticateRequest = new AuthenticationRequest();
                        mAuthenticateRequest.setAppId(client.getAppUid());
                        mAuthenticateRequest.setAppSecret(client.getAppSecret());
                        mAuthenticateRequest.setPermissions(mPermissions);

                        client.authenticate(100, mAuthenticateRequest);
                    }
                });
            }
            ...
        });
    }