Amazon web services 如何检查dynamodb表中是否存在项?

Amazon web services 如何检查dynamodb表中是否存在项?,amazon-web-services,amazon-dynamodb,user-data,Amazon Web Services,Amazon Dynamodb,User Data,我正在制作一个android应用程序,通过facebook登录并定制注册。我正在使用AWS dynamodb来存储用户数据 我可以存储facebook和自定义注册的数据,但无法扫描这些数据。事实上,我希望每当用户使用其自定义或facebook凭据返回登录时,应用程序应检查输入的字段是否存在于表中。若不可用,应用程序将要求用户先注册 main活动 public class MainActivity extends AppCompatActivity implements View.OnClickL

我正在制作一个android应用程序,通过facebook登录并定制注册。我正在使用AWS dynamodb来存储用户数据

我可以存储facebook和自定义注册的数据,但无法扫描这些数据。事实上,我希望每当用户使用其自定义或facebook凭据返回登录时,应用程序应检查输入的字段是否存在于表中。若不可用,应用程序将要求用户先注册

main活动

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    Button login;
    TextView signup;
    TextView help;
    EditText etUsername;
    EditText etPassword;
    String email;
    String pass;

    String email1;
    String pass1;
    private CognitoCachingCredentialsProvider credentialsProvider;

    private CallbackManager callbackManager;
    private LoginButton loginButton;
    private ImageButton btnLoginFb;
    private ProgressDialog progressDialog;
    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        etUsername = (EditText) findViewById(R.id.etUsername);

        etPassword = (EditText) findViewById(R.id.etPassword);

        login = (Button) findViewById(R.id.loginbutton);
        signup = (TextView) findViewById(R.id.textViewsignup);
        help = (TextView) findViewById(R.id.textViewHelp);
        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);
        login.setOnClickListener(this);
        signup.setOnClickListener(this);
        help.setOnClickListener(this);

        Context mContext = this.getApplicationContext();
        credentialsProvider = new CognitoCachingCredentialsProvider(
                mContext, // get the context for the current activity
                "us-east-1:*******************************",
                Regions.US_EAST_1
        );

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginbutton:
                email = etUsername.getText().toString();
                pass = etPassword.getText().toString();

                AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(credentialsProvider);
                DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);



                if (email != null && pass != null) {

                        Intent slideactivity = new Intent(MainActivity.this, Welcome.class);

                        Bundle bndlanimation =
                                ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.animation, R.anim.animation2).toBundle();
                        startActivity(slideactivity, bndlanimation);
                    return;
                }                     
                else {
                    AlertDialog alertDialog = new AlertDialog.Builder(
                            MainActivity.this).create();

                    // Setting Dialog Title
                    alertDialog.setTitle("Oops");

                    // Setting Dialog Message
                    alertDialog.setMessage("No data found. You have to signup first!!!");

                    // Setting Icon to Dialog
                    //alertDialog.setIcon(R.drawable.tick);

                    // Setting OK Button
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // Write your code here to execute after dialog closed
                            startActivity(new Intent(MainActivity.this, SignUp.class));
                        }
                    });

                    // Showing Alert Message
                    alertDialog.show();

                }
                break;
            case R.id.textViewsignup:
                Intent slideactivity = new Intent(MainActivity.this, SignUp.class);

                Bundle bndlanimation =
                        ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.animation, R.anim.animation2).toBundle();
                startActivity(slideactivity, bndlanimation);

                break;

            case R.id.textViewHelp:
                Intent slideactivity1 = new Intent(MainActivity.this, LoginHelp.class);

                Bundle bndlanimation1 =
                        ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.animation, R.anim.animation2).toBundle();
                startActivity(slideactivity1, bndlanimation1);

                break;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();


        callbackManager = CallbackManager.Factory.create();

        loginButton = (LoginButton) findViewById(R.id.login_button);

        loginButton.setReadPermissions("public_profile", "email", "user_friends");

        btnLoginFb = (ImageButton) findViewById(R.id.btnLoginFb);
        btnLoginFb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Loading...");
                progressDialog.show();

                loginButton.performClick();

                loginButton.setPressed(true);

                loginButton.invalidate();

                loginButton.registerCallback(callbackManager, mCallBack);

                loginButton.setPressed(false);

                loginButton.invalidate();

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }


    private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            progressDialog.dismiss();

            // App code
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {

                            Log.e("response: ", response + "");

                            try {

                                user = new User();
                                user.facebookID = object.getString("id").toString();
                                pass = user.facebookID;
                                Log.e(pass, "id");
                                user.email = object.getString("email").toString();
                                email = user.email;
                                Log.e(email, "email");
                                user.name = object.getString("name").toString();
                                user.gender = object.getString("gender").toString();
                                PrefUtils.setCurrentUser(user, MainActivity.this);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            Toast.makeText(MainActivity.this, "welcome " + user.name, Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(MainActivity.this, Welcome.class);
                            startActivity(intent);
                            finish();

                        }

                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender, birthday");
            request.setParameters(parameters);
            request.executeAsync();
            new db().execute("");

        }

        @Override
        public void onCancel() {
            progressDialog.dismiss();
        }

        @Override
        public void onError(FacebookException e) {
            progressDialog.dismiss();
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class db extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(credentialsProvider);
            DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);
            Item item = new Item();
            mapper.load(Item.class, email, pass);
            if(item==null)
            {
                startActivity(new Intent(MainActivity.this,SignUp.class));
            }
            else{
                item.setEmail(email);
                item.setPass(pass);
                mapper.save(item);
                startActivity(new Intent(MainActivity.this,Welcome.class));
            }
           mapper.load(Item.class, email,pass);
           if(item==null)           {
                startActivity(new Intent(MainActivity.this,SignUp.class));
          }
            else{
               startActivity(new Intent(MainActivity.this,Welcome.class));
           }
            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {

        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }
public类MainActivity扩展AppCompatActivity实现View.OnClickListener{
私有静态最终字符串TAG=“MainActivity”;
按钮登录;
文本视图注册;
文本视图帮助;
编辑文本和用户名;
编辑文本和密码;
字符串电子邮件;
串通;
字符串1;
字符串pass1;
私人CognitocachingCredentialsProviderCredentialsProvider;
私人CallbackManager CallbackManager;
私人登录按钮登录按钮;
私有图像按钮btnLoginFb;
私有进程对话;
用户;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
sdkinInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
etUsername=(EditText)findViewById(R.id.etUsername);
etPassword=(EditText)findViewById(R.id.etPassword);
login=(按钮)findviewbyd(R.id.loginbutton);
注册=(TextView)findViewById(R.id.textViewsignup);
help=(TextView)findViewById(R.id.textViewHelp);
etUsername=(EditText)findViewById(R.id.etUsername);
etPassword=(EditText)findViewById(R.id.etPassword);
login.setOnClickListener(this);
signup.setOnClickListener(this);
help.setOnClickListener(this);
Context mContext=this.getApplicationContext();
credentialsProvider=新CognitoCachingCredentialsProvider(
mContext,//获取当前活动的上下文
“us-east-1:*************************************”,
美国东部地区1
);
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.loginbutton:
email=etUsername.getText().toString();
pass=etPassword.getText().toString();
AmazondynamodClient ddbClient=新的AmazondynamodClient(凭证提供者);
DynamoDBMapper映射器=新的DynamoDBMapper(DDB客户端);
如果(电子邮件!=null&&pass!=null){
Intent slideactivity=newintent(MainActivity.this、Welcome.class);
捆绑式动画=
ActivityOptions.makeCustomAnimation(getApplicationContext(),R.anim.animation,R.anim.animation2).toBundle();
startActivity(slideactivity、bndlanimation);
返回;
}                     
否则{
AlertDialog AlertDialog=新建AlertDialog.Builder(
MainActivity.this.create();
//设置对话框标题
alertDialog.setTitle(“Oops”);
//设置对话框消息
setMessage(“找不到数据,您必须先注册!!!”;
//将图标设置为对话框
//alertDialog.setIcon(R.drawable.tick);
//设置OK按钮
alertDialog.setButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
//在此处编写代码,以便在对话框关闭后执行
startActivity(新意图(MainActivity.this、SignUp.class));
}
});
//显示警报消息
alertDialog.show();
}
打破
案例R.id.textViewsignup:
Intent slideactivity=newintent(MainActivity.this、SignUp.class);
捆绑式动画=
ActivityOptions.makeCustomAnimation(getApplicationContext(),R.anim.animation,R.anim.animation2).toBundle();
startActivity(slideactivity、bndlanimation);
打破
案例R.id.textViewHelp:
意向slideactivity1=新意向(MainActivity.this,LoginHelp.class);
束bndlanimation1=
ActivityOptions.makeCustomAnimation(getApplicationContext(),R.anim.animation,R.anim.animation2).toBundle();
startActivity(slideactivity1,bndlanimation1);
打破
}
}
@凌驾
受保护的void onResume(){
super.onResume();
callbackManager=callbackManager.Factory.create();
loginButton=(loginButton)findviewbyd(R.id.login_按钮);
setReadPermissions(“公共档案”、“电子邮件”、“用户朋友”);
btnLoginFb=(ImageButton)findViewById(R.id.btnLoginFb);
btnLoginFb.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
progressDialog=新建progressDialog(MainActivity.this);
progressDialog.setMessage(“加载…”);
progressDialog.show();
loginButton.performClick();
loginButton.setPressed(true);
loginButton.invalidate();
registerCallback(callbackManager,mCallBack);
loginButton.setPressed(假);
loginButton.invalidate();
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResu
    AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(credentialsProvider);
    DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);

    // Use the password as the third parameter if it is a range key.
    Item item = mapper.load(Item.class, email1);

    if(item == null){
        // That email is not in the database
    }
    else{
        // Does exist in database, now compare password.
    }