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

效果图:

Android之Bmob移动后端云服务器的更多相关文章

  1. Bmob 移动后端云服务器平台实现登录注册

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

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

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

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

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

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

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

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

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

  6. 基于后端云的Android注册登录开发

    APP开发离不开注册登录功能,但是注册登录功能开发需要后台数据库的支持,对于一些初学者或者对后台数据 不熟悉的同学来说可能会有些困难.本文介绍一下后端云: 1. Bmob是国内起步较早的云后端服务平台 ...

  7. $微信小程序开发实践点滴——接入Bmob后端云

    Bmob后端云官网:http://www.bmob.cn/ 微信公众平台官网:https://mp.weixin.qq.com/ 微信小程序官方开发文档:https://mp.weixin.qq.co ...

  8. 基于bmob后端云小程序开发——口袋吉他

    人的一生90%的时间都在做着无聊的事情,社会的发展使得我们的闲暇时间越来越多,我们把除了工作的其他时间放在各种娱乐活动上. 程序员有点特殊,他们把敲代码看成娱乐活动的一部分,以此打发时间的不占少数.这 ...

  9. Android中实现java与PHP服务器(基于新浪云免费云平台)http通信详解

    Android中实现java与PHP服务器(基于新浪云免费云平台)http通信详解 (本文转自: http://blog.csdn.net/yinhaide/article/details/44756 ...

随机推荐

  1. 谈谈调用腾讯云【OCR-通用印刷体识别】Api踩的坑

    一.写在前面 最近做项目需要用到识别图片中文字的功能,本来用的Tesseract这个写的,不过效果不是很理想. 随后上网搜了一下OCR接口,就准备使用腾讯云.百度的OCR接口试一下效果.不过这个腾讯云 ...

  2. JIRA 敏捷开发平台部署记录

    1.1 jira说明 JIRA是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领域. JIRA中配置灵活.功能全 ...

  3. 【Jquery系列】之DOM属性

    1   概述 本章将结合JQuery官方API,对Jquery属性进行分析与讲解.主要讲.addClass(),.attr(),,hasClass(),,html(),.prop(),.removeA ...

  4. 【转】Win10下 python3和python2同时安装并解决pip共存问题

    1.下载python3和python2 进入python官网,链接https://www.python.org/ 选择Downloads--->Windows,点击进入就可以看到寻找想要的pyt ...

  5. 自学Python5.3-内置模块(1)

    内置模块(1)内置模块是Python自带的功能,在使用内置模块相应的功能时,需要  先导入 再 使用  1.OS模块       用于提供系统级别的操作: os.getcwd() 获取当前工作目录,即 ...

  6. 解决jquery.zclip.js插件无法复制的问题

    网页中需要用到点击复制,在使用webpack加载jquery以及jquery.zclip.js后,出现了以下情况: jquery顺利加载 zclip插件顺利加载 ZeroClipboard.swf顺利 ...

  7. sqlserver 存储过程 增加

    CREATE PROCEDURE [dbo].[InsertMessage]( @strTable varchar(), --表名 @strValues nvarchar(), --要插入的数据(用英 ...

  8. C#Lambda表达式Aggregate的用法及内部运行方式的猜想

    , , , , }; // 其和为15 var count = nums.Aggregate((body, next) => { // 注意,nums的元素个数至少一个以上(但如果是有seed的 ...

  9. 快速自检电脑是否被黑客入侵过(Linux版)

    之前写了一篇快速自检电脑是否被黑客入侵过(Windows版), 这次就来写写Linux版本的. 前言 严谨地说, Linux只是一个内核, GNU Linux才算完整的操作系统, 但在本文里还是用通俗 ...

  10. 解题思路:best time to buy and sell stock i && ii && iii

    这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...