源码下载: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","更改成功");
                                    }
                                }

效果图:

Bmob 移动后端云服务器平台实现登录注册的更多相关文章

  1. Android之Bmob移动后端云服务器

    源码下载:http://download.csdn.net/download/jjhahage/10034519 PS:一般情况下,我们在写android程序的时候,想要实现登录注册功能,可以选择自己 ...

  2. Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能

    Android高效率编码-第三方SDK详解系列(二)--Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能 我的本意是第二篇写Mob的shareSD ...

  3. Bmob移动后端云服务平台--Android从零開始--(二)android高速入门

    Bmob移动后端云服务平台--Android从零開始--(二)android高速入门 上一篇博文我们简介何为Bmob移动后端服务平台,以及其相关功能和优势. 本文将利用Bmob高速实现简单样例,进一步 ...

  4. Bmob移动后端云服务平台--Android从零開始--(一)何为Bmob

    Bmob移动后端云服务平台--Android从零開始--(一)何为Bmob 在正式的项目开发中,单client不能满足我们的需求,须要实现client与服务端的连接. 而在编写Android服务端代码 ...

  5. Bmob—移动后端云服务平台

    对于个人或者小团队来说,开发一个有网络功能的游戏是一件不容易的事情,必须掌握一门诸如Java/.net/php这类的服务器开发语言. Bmob云服务方便了开发者.Bmob可以给应用软件快速添加一个安全 ...

  6. 阿里云服务器教程–SSH 登录时出现如下错误:Host key verification failed

    注意:本文相关 Linux 配置及说明已在 CentOS 6.5 64 位操作系统中进行过测试.其它类型及版本操作系统配置可能有所差异,具体情况请参阅相应操作系统官方文档. 问题描述 使用 SSH 登 ...

  7. 云服务器用ssh登录

    本地生成密钥 这里选择在购买前先在本地生成密钥key(分为private key和public key),所以先生成密钥再进行购买,购买完以后直接ssh登录,不需要输入密码(安全性考虑) 其中,pub ...

  8. 无法登录到Windows云服务器怎么办?

    当您的云服务器无法远程登录时,我们首先建议您使用VNC方式登录. 是否可以通过控制台远程登录 远程登录失败时,请首先尝试能否通过管理控制台,使用VNC方式登录弹性云服务器. 登录管理控制台. 选择“计 ...

  9. 亚马逊云服务器AWS安装CentOS

    亚马逊云服务器默认创建的实例,在停止之后再启动的情况下,IP会发生改变.所以我们最好先创建一个弹性IP,即EIP,不过我也不清楚这个费用. 1.按如图操作创建一个弹性IP,弹性IP创建之后可以随便绑定 ...

随机推荐

  1. 微软云计算 Massive Data 处理语言Scope 1

    Massive Data处理一直是云计算中很重要的一个环节.目前像Google,Yahoo在相关方面都有自己专有的技术.例如Google的基于MapReduce的Sawzall语言.和Yahoo基于H ...

  2. A workaround to change shared memory size for Docker containers in AWS ECS

    Issue Because of not supporting to specify the following docker run parameter, containers in ECS can ...

  3. ThinkPHP中对系统常量的使用

    /Wen 当前模块的URL地址 /Wen/postname 当前操作的URL地址 /thinkphp-changliang/ 当前URL地址 APP_DEBUG 是否开启调试模式(框架两种模式:[默认 ...

  4. springmvc中对日期格式化的处理

    @DateTimeFormat(pattern="yyyy-MM-dd") 返回的时候java.util.Date pattern="yyyy-MM-dd"必须 ...

  5. Nova控制节点集群

    #Nova控制节点集群 openstack pike 部署 目录汇总 http://www.cnblogs.com/elvi/p/7613861.html ##Nova控制节点集群 # control ...

  6. SPOJ SERGRID - Grid BFS

    SERGRID - Grid no tags  You are on an nxm grid where each square on the grid has a digit on it. From ...

  7. python学习之路day2

    模块学习: 标准库: os: 第三方库:

  8. UWP 共享文件——发送者

    这一节,顾名思义,即使你要共享数据给别人,你是数据的提供者.分两步即可1.直接复制代码 protected override void OnNavigatedTo(NavigationEventArg ...

  9. Zepto中的Swipe事件失效

    需要阻止浏览器默认滑动的事件 document.addEventListener('touchmove', function (event) { event.preventDefault(); }, ...

  10. Linux基础学习笔记以及常用命令

    1.windows自带命令进入mysql所在磁盘   2.进入mysql安装目录的bin文件 D:\>cd D:\Program Files (x86)\mysql-5.5.25-winx64\ ...