源码下载:http://download.csdn.net/download/jjhahage/10034519
PS:一般情况下,我们在写android程序的时候,想要实现登录注册功能,可以选择自己用servlet作为服务端来实现过滤没有注册过的用户,但是太麻烦,而且不是随时都可以用的。这里介绍一个移动后端云服务器平台bmob,这不仅可以实现云数据库储存,还可以获取手机验证等,随时随地都很轻松,下面写一个小demo,实现一个登陆注册功能,认识增删查改。
1:首先到bmob官网,注册一个账号,里面创建一个项目,如图:
2:创建一个android项目,(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'
位置如图:
(2)添加权限:
<!--允许联网-->
<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):添加maven,到指定的云库
maven { url "https://raw.github.com/bmob/bmob-android-sdk/master"}
(4:)初始化:
Bmob.initialize(this,"你的 应用ID");
3:下面就是代码了写一个实体类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;
}
}
写三个布局,分别是注册页面,登录页面,登录成功跳转的页面
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>
register_layout.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_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>
注册Activity,RegisterActivity.java 功能:增
这里是一个简单的注册,里面没有加判断,所以,一个号可以重复注册,但是只有唯一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);
}
}
登录页面:MainActivity.java 功能:查
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);
}
}
登录成功页面 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);
}
}
总结:
唯一id的获取可以通过用户名来获取,当用户输入用户名时,只要数据库中用户名和输入的一致,则就可以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","更改成功");
}
}
效果图: