前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android中的Bmob移动后端云服务器功能

Android中的Bmob移动后端云服务器功能

作者头像
砸漏
发布2020-10-31 17:03:38
2.8K0
发布2020-10-31 17:03:38
举报
文章被收录于专栏:恩蓝脚本

源码下载:http://xiazai.zalou.cn/201801/yuanma/BmobTest1.rar

PS:一般情况下,我们在写android程序的时候,想要实现登录注册功能,可以选择自己用servlet作为服务端来实现过滤没有注册过的用户,但是太麻烦,而且不是随时都可以用的。这里介绍一个移动后端云服务器平台bmob,这不仅可以实现云数据库储存,还可以获取手机验证等,随时随地都很轻松,下面写一个小demo,实现一个登陆注册功能,认识增删查改。下面我稍微写一个例子,简单实现注册登录功能。

1:首先到bmob官网,注册一个账号,里面创建一个项目,如图:

2:创建一个android项目,(AndroidStudio)

(1):添加依赖:在app下的build.gradle中添加

代码语言:javascript
复制
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)添加权限:

代码语言:javascript
复制
<!--允许联网-- 
<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,

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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,先把各页面都写了,后续就好办了。

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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。

代码语言:javascript
复制
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 功能:查

代码语言:javascript
复制
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

代码语言:javascript
复制
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()

处理增删查改

代码语言:javascript
复制
增:
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移动后端云服务器功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对ZaLou.Cn网站的支持!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档