如何从房间数据库发送表信息后检查表是否为空?

时间:2018-08-31 14:04:51

标签: java android android-database

我在一个基于该教程的项目上进行工作,该教程使用facebook工具包进行了登录会话。现在我已经删除了facebook工具包并制定了自己的注册计划,这是注册工作的方式:

如果用户不存在:

获取电话=>使用短信验证代码=>继续应用

如果用户不存在:

获取电话=>使用短信验证代码=>注册信息=>将电话保存在会议室数据库中=>继续应用。

现在在应用程序中,我们不再进行直接注册,我们让人们使用应用程序,当他们想付款时,他们应该进入登录/注册页面。这个Facebook工具包使用的是getAccessToken。现在我想要的是将phoneNumber保存在数据库中,如果存在,则将其发送到服务器。

//在阅读代码部分之后:如您所见,facebook使用令牌,然后获取电话,然后将其发送到服务器,现在我想检查一下电话表是否不为空,然后获取电话并发送电话服务器,以获取用户信息。

public static UserRepository userRepository;

因此,在进行一切操作之前,请先检查使用代码注册的结果:

try {

        userDatabase userPhone = new userDatabase();
        userPhone.setPhoneNumber(phone);

        Common.userRepository.insertPhone(userPhone);

        Log.d("ATLAS_DBADD", new Gson().toJson(userPhone));

    }
    catch (Exception ex)
    {
        Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
    }
// Log in logcat base on Log.d
 08-31 17:46:26.657 1937-1937/ir.atlaspio.atlasdrinkingservice D/ATLAS_DBADD: {"phoneNumber":"09361523172","uid":0}

这是整个Room数据库userDatabase.java:

@Entity(tableName = "userDatabase")
public class userDatabase  {

@PrimaryKey
@NonNull
private int uid;

public String phoneNumber;

public userDatabase() {
}

public int getUid() {
    return uid;
}

public void setUid(int uid) {
    this.uid = uid;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(@NonNull String phoneNumber) {
    this.phoneNumber = phoneNumber;
}
}

UserDatabaseDao:

@Dao
public interface UserDatabaseDao {


@Insert(onConflict = OnConflictStrategy.REPLACE)
long insertPhone(userDatabase userDatabase);

@Update
public void updatePhone(userDatabase userDatabase);

@Delete
public void deletePhone(userDatabase userDatabase);

@Query("SELECT * FROM userDatabase WHERE uid = :userId")
public userDatabase getPhoneById(String userId);

}

UserRepository.java:

public class UserRepository implements IUserDataSource{

private IUserDataSource iUserDataSource;



public UserRepository(IUserDataSource iUserDataSource) {
    this.iUserDataSource = iUserDataSource;
}

private static UserRepository instance;

public static UserRepository getInstance(IUserDataSource iUserDataSource){
    if (instance == null)
    {
        instance = new UserRepository(iUserDataSource);
    }
    return instance;
}

@Override
public long insertPhone(userDatabase userDatabase) {
    return iUserDataSource.insertPhone(userDatabase);
}

@Override
public void updatePhone(userDatabase userDatabase) {
    iUserDataSource.updatePhone(userDatabase);

}

@Override
public void deletePhone(userDatabase userDatabase) {
    iUserDataSource.deletePhone(userDatabase);
}

@Override
public userDatabase getPhoneById(String userId) {
    return iUserDataSource.getPhoneById(userId);
}
}

IUserDataSource接口:

public interface IUserDataSource {


long insertPhone(userDatabase userDatabase);

public void updatePhone(userDatabase userDatabase);

public void deletePhone(userDatabase userDatabase);

public userDatabase getPhoneById(String userId);
}

ATLASRoomDatabase.java:

@Database(entities = {Cart.class, Favorite.class, userDatabase.class},version = 1,exportSchema = false)
public abstract class ATLASRoomDatabase extends RoomDatabase {


public abstract CartDAO cartDAO();
public abstract FavoriteDAO favoriteDAO();
public abstract UserDatabaseDao userDbDAO();

private static ATLASRoomDatabase instance;

public static ATLASRoomDatabase getInstance(Context context)
{
    if (instance == null)
        instance = Room.databaseBuilder(context,ATLASRoomDatabase.class,"ATLASCO_Pasargad_DrinkShopDB")
                .allowMainThreadQueries()
                .build();
    return instance;
}

}

然后我将带facebook工具包和不带facebook工具包放在一处:

Without facebook kit :
private void checkSessionLogin() {

    userDatabase userPhone = new userDatabase();


    if ((userPhone.getPhoneNumber() == null)){



        swipeRefreshLayout.setRefreshing(true);
        //Check exists user on Serve (MySQL)
        mService.checkExistsUser(userPhone.getPhoneNumber())
                .enqueue(new Callback<CheckUserResponse>() {
                    @Override
                    public void onResponse(Call<CheckUserResponse> call, Response<CheckUserResponse> response) {
                        CheckUserResponse userResponse = response.body();
                        if (userResponse.isExists())
                        {
                            //Request information of current user
                            mService.getUserInformation(Common.currentUser.getPhone())
                                    .enqueue(new Callback<User>() {
                                        @Override
                                        public void onResponse(Call<User> call, Response<User> response) {
                                            Common.currentUser = response.body();
                                            if (Common.currentUser != null) {
                                                swipeRefreshLayout.setRefreshing(false);



                                                //Set Info
                                                txt_name.setText(Common.currentUser.getName());
                                                txt_phone.setText(Common.currentUser.getPhone());

                                                //Set Avatar
                                                if (!TextUtils.isEmpty(Common.currentUser.getAvatarUrl())) {
                                                    Picasso.with(getBaseContext())
                                                            .load(new StringBuilder(Common.BASE_URL)
                                                                    .append("user_avatar/")
                                                                    .append(Common.currentUser.getAvatarUrl()).toString())
                                                            .into(img_avatar);
                                                }

                                            }
                                        }

                                        @Override
                                        public void onFailure(Call<User> call, Throwable t) {
                                            swipeRefreshLayout.setRefreshing(false);
                                            Log.d("ERROR",t.getMessage());

                                        }
                                    });
                        }
                    }

                    @Override
                    public void onFailure(Call<CheckUserResponse> call, Throwable t) {

                    }
                });
    }

}

// With facebook
private void checkSessionLogin() {
    if (AccountKit.getCurrentAccessToken() != null)
    {
        swipeRefreshLayout.setRefreshing(true);

        //Check exists user on Serve (MySQL)
        AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
            @Override
            public void onSuccess(final Account account) {
                mService.checkExistsUser(account.getPhoneNumber().toString())
                        .enqueue(new Callback<CheckUserResponse>() {
                            @Override
                            public void onResponse(Call<CheckUserResponse> call, Response<CheckUserResponse> response) {
                                CheckUserResponse userResponse = response.body();
                                if (userResponse.isExists())
                                {
                                    //Request information of current user
                                    mService.getUserInformation(account.getPhoneNumber().toString())
                                            .enqueue(new Callback<User>() {
                                                @Override
                                                public void onResponse(Call<User> call, Response<User> response) {
                                                    Common.currentUser = response.body();
                                                    if (Common.currentUser != null) {
                                                        swipeRefreshLayout.setRefreshing(false);



                                                        //Set Info
                                                        txt_name.setText(Common.currentUser.getName());
                                                        txt_phone.setText(Common.currentUser.getPhone());

                                                        //Set Avatar
                                                        if (!TextUtils.isEmpty(Common.currentUser.getAvatarUrl())) {
                                                            Picasso.with(getBaseContext())
                                                                    .load(new StringBuilder(Common.BASE_URL)
                                                                            .append("user_avatar/")
                                                                            .append(Common.currentUser.getAvatarUrl()).toString())
                                                                    .into(img_avatar);
                                                        }

                                                    }
                                                }

                                                @Override
                                                public void onFailure(Call<User> call, Throwable t) {
                                                    swipeRefreshLayout.setRefreshing(false);
                                                    Log.d("ERROR",t.getMessage());

                                                }
                                            });
                                }
                                else
                                {
                                    //if user not exists on Database , just make login
                                    startActivity(new Intent(HomeActivity.this,MainActivity.class));
                                    finish();
                                }
                            }

                            @Override
                            public void onFailure(Call<CheckUserResponse> call, Throwable t) {
                                Log.d("ERROR",t.getMessage());
                            }
                        });
            }

            @Override
            public void onError(AccountKitError accountKitError) {
                Log.d("ERROR",accountKitError.getErrorType().getMessage());
            }
        });
    }
    else
    {
        AccountKit.logOut();

        //Clear All Activity
        Intent intent = new Intent(HomeActivity.this,MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }
}

0 个答案:

没有答案