PS: Under normal circumstances, when we write android programs, if we want to realize the login and registration function, we can choose to use servlet as the server to filter unregistered users, but it is too troublesome and not always available. Here is a mobile back-end cloud server platform bmob, which can not only realize cloud database storage, but also obtain mobile phone verification, etc., which is very easy anytime, anywhere. Write a small demo below to realize a login and registration function, and understand adding, deleting, checking and modifying. Next, I will write a little example to simply realize the registration and login function.
1: First go to the bmob official website, register an account, and create a project in it, as shown in the figure:
2: Create an android project, (AndroidStudio)
(1):添加依赖:在app下的build.gradle中添加
compile 'cn.bmob.android:bmob-sdk:3.4.6'
compile 'com.squareup.okhttp:okhttp:2.4.0'//CDN文件服务使用okhttp相关包进行文件的上传和下载(必填)
compile 'com.squareup.okio:okio:1.4.0'
sourceSets {
main.jniLibs.srcDirs = ['libs']
}
useLibrary 'org.apache.http.legacy'
The location is as shown in the figure:
(2) Add permissions:
<!--允许联网-->
<uses-permission android:name="android.permission.INTERNET"/>
<!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--获取wifi网络状态的信息-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<!--保持CPU运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载-->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<!--获取sd卡写的权限,用于文件上传和下载-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--允许读取手机状态 用于创建BmobInstallation-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
(3): Add maven to the specified cloud library
maven {
url "https://raw.github.com/bmob/bmob-android-sdk/master"}
(4:)初始化:
Bmob.initialize(this,"your application ID");
3: The following is the code
Write an entity class person,
package cn.day1.model;
import cn.bmob.v3.BmobObject;
/**
* Created by CMusketeer on 17/10/22.
*/
public class Person extends BmobObject {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Write three layouts, namely the registration page, the login page, and the page that the login successfully jumps to
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.day1.bmobtest1.MainActivity">
<TextView
android:gravity="center"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<EditText
android:id="@+id/id_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="username"/>
<EditText
android:id="@+id/id_userpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/id_ok"
android:layout_width="0dp"
android:text="登录"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/id_register"
android:text="注册"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Registration page: register_layout.xml, write all the pages first, and then it will be easy to handle later.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.day1.bmobtest1.MainActivity">
<TextView
android:gravity="center"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册中心" />
<EditText
android:id="@+id/id_register_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="username"/>
<EditText
android:id="@+id/id_register_userpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/id_register_ok"
android:text="注册"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
登录成功页面:success.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="成功登录"
android:gravity="center"
android:textSize="50dp"/>
</LinearLayout>
Register Activity, RegisterActivity.java function: increase
Here is a simple registration without judgment, so a number can be registered repeatedly, but only has a unique ID.
package cn.day1.bmobtest1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.bmob.v3.listener.SaveListener;
import cn.day1.model.Person;
/**
* Created by CMusketeer on 17/10/22.
*/
public class RegisterActivity extends Activity {
private TextView register_user;
private TextView register_password;
private Button register_ok;
private Person p2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_layout);
addControl();//加载控件
addRegisterShow();//注册方法
}
private void addRegisterShow() {
register_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String rUser=register_user.getText().toString().trim();
String rPassword=register_password.getText().toString().trim();
//判断用户名和密码是否为空,如果为空则不能进去。
if(rUser.length()>0&&rPassword.length()>0){
p2 = new Person();
p2.setName(rUser);
p2.setPassword(rPassword);
//插入方法
p2.save(RegisterActivity.this, new SaveListener() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
register_password.setText("");
register_user.setText("");
Toast.makeText(RegisterActivity.this, "添加数据成功,返回objectId为:" + p2.getObjectId(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int code, String msg) {
// TODO Auto-generated method stub
Toast.makeText(RegisterActivity.this, "创建数据失败:" + msg, Toast.LENGTH_SHORT).show();
}
});
}else{
Toast.makeText(RegisterActivity.this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
}
}
});
}
private void addControl() {
register_user = (TextView) findViewById(R.id.id_register_username);
register_password = (TextView) findViewById(R.id.id_register_userpassword);
register_ok = (Button) findViewById(R.id.id_register_ok);
}
}
Login page: MainActivity.java Function: check
package cn.day1.bmobtest1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.day1.model.Person;
public class MainActivity extends AppCompatActivity {
private Person p2;
private TextView lgUser;
private TextView lgPassword;
private Button btn_ok;
private Button btn_rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bmob.initialize(this, "你的 应用id");
setContentView(R.layout.activity_main);
addControl();
addLogin();
}
private void addLogin() {
btn_rg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,RegisterActivity.class);
startActivity(intent);
}
});
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BmobQuery<Person> query=new BmobQuery<Person>();
query.findObjects(MainActivity.this,new FindListener<Person>(){
String lgU=lgUser.getText().toString().trim();
String lgp=lgPassword.getText().toString().trim();
int panduan=1;
@Override
public void onSuccess(List<Person> list) {
for(int i=0;i<list.size();i++){
String name=list.get(i).getName();
String password=list.get(i).getPassword();
Log.e("user","唯一 id:"+list.get(i).getObjectId()+"----"+name+"---"+password);
if(name.equals(lgU) && password.equals(lgp)){
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
panduan=2;
//成功后panduan等于2,则跳出该循环,并且把输入快都清空,跳转到指定页面
lgUser.setText("");
lgPassword.setText("");
Intent intent=new Intent(MainActivity.this,Success.class);
startActivity(intent);
break;
}
}
if(panduan==1){
Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onError(int i, String s) {
}
});
}
});
}
private void addControl() {
lgUser = (TextView) findViewById(R.id.id_username);
lgPassword = (TextView) findViewById(R.id.id_userpassword);
btn_ok = (Button) findViewById(R.id.id_ok);
btn_rg = (Button) findViewById(R.id.id_register);
}
}
Login success page Success.java
package cn.day1.bmobtest1;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by CMusketeer on 17/10/22.
*/
public class Success extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
}
}
Summarize:
The unique id can be obtained through the user name. When the user enters the user name, as long as the user name in the database is consistent with the input, you can list.get(i).getObjectId()
处理增删查改
增:
person = new Person();
person.setName(user);
person.setAddress(password);
person.save(new SaveListener<String>() {
@Override
public void done(String s, BmobException e) {
if(e == null){
Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
}
else{
}
}
});
删
Id可以通过查处所有的,从而得到id
id=list.get(i).getObjectId();
person = new Person();
person.delete(id, new UpdateListener() {
@Override
public void done(BmobException e) {
if(e==null){
Log.e("sss","删除成功"); }
}
});
查 :和上面的查不大一样,这也是一种方法
//查询所有,
query.findObjects(new FindListener<Person>() {
@Override
public void done(List<Person> list, BmobException e) {
}}
//查询单个
query.getObject(id,new listener)
改
person.setName(“111”);
person.update(id,new UpdateListener() {
@Override
public void done(BmobException e) {
if(e==null){
// Toast.makeText(MainActivity.this, "更改成功", Toast.LENGTH_SHORT).show();
Log.e("sss","更改成功");
}
}
Renderings: