利用Bmob快速实现安卓的简单登陆注册功能Bmobsdk3.6.9版本亲测可用(详细图文攻略附带处理小bug)

前言:从其他人的留言中我知道Bmob这个云数据库,之前我一直在自己写后台但是发现太慢了要学的东西超级多,脚手架也不知道为什么老是搭不上很蛋疼,然后春节又必须回来老家山区里面有些事情耽搁了后台也凉了,后面我知道了Bmob所以决定学一下这个。因为要在一个月之内做完一个拥有滤镜美颜+聊天的app确实有点麻烦又是一个人独立研究开发。
这个是项目的GITHUB地址:https://github.com/307572384/bmobtest
如果觉得不错麻烦给个star

在这里插入图片描述
好了话不多说我们立马来开始我们的功能实现之旅

如果Bmob没有配置请参考:http://doc.bmob.cn/data/android/

我们先来完成一个布局

activity_main
很简单一个登陆注册该有的都有

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.beta.bmobtest.MainActivity"
    android:orientation="vertical">



    <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 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"
    android:orientation="vertical">


    <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 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>

接着我们来看RegisterActivity

package com.beta.bmobtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Kevein on 2019/3/1.8:03
 */

public class RegisterActivity extends Activity {
	private TextView register_user;
	private TextView register_password;
	private Button   register_ok;
	private Person   p2;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_layout);
		addControl();//加载控件
		addRegisterShow();//注册方法
		Bmob.initialize(this, "****这里填你自己的APPid****");
	}
//Bmob的注册方法
	private void addRegisterShow() {
		register_ok.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final BmobUser p2 = new BmobUser();
				p2.setUsername(register_user.getText().toString());
				p2.setPassword(register_password.getText().toString());
				//插入方法
				p2.signUp(new SaveListener<BmobUser>() {
					@Override
					public void done(BmobUser bmobUser, BmobException e) {
						if (e == null) {
							//判断是否注册成功成功则跳转到登陆的页面
							Intent intent_register = new Intent(RegisterActivity.this,MainActivity.class);
							startActivity(intent_register);
							Toast.makeText(RegisterActivity.this, "添加数据成功,返回objectId为:" + p2.getObjectId(), Toast.LENGTH_SHORT).show();
							//这里我们需要了解一下什么objectid就像是一个登陆校验一样只要有这个就代表着你往表里面写入的数据是成功的了。
						} 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

package com.beta.bmobtest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

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);
		setContentView(R.layout.activity_main);

		Bmob.initialize(this, "你的appid");


		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);
			}
		});
//Bmob登陆方法
		btn_ok.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String lgU = lgUser.getText().toString().trim();
				String lgp = lgPassword.getText().toString().trim();
				final BmobUser bu2 = new BmobUser();
				bu2.setUsername(lgU);
				bu2.setPassword(lgp);
				bu2.login(new SaveListener<BmobUser>() {
					@Override
					public void done(BmobUser bmobUser, BmobException e) {
						if (e == null) {
						//登陆成功就跳转到主页面
							Intent in_success = new Intent(MainActivity.this, Success.class);
							startActivity(in_success);

						} else {
							Toast.makeText(MainActivity.this, "账户名或密码不正确", Toast.LENGTH_SHORT).show();
							//loge(e);
						}

					}
				});


			}


		});
	}

	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页面也是最简单的一个页面

package com.beta.bmobtest;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;

/**
 * Created by Kevein on 2019/3/1.8:39
 */

public class Success extends Activity {
	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.success);
	}
}

好了至此我们就完成了Bmob实现利用Bmob快速实现安卓的简单登陆注册功能

最后总结一些问题:

配置ContentProvider千万不能忘记我就是忘记这个然后就搞了2天,最后才发现是在另一篇文章中有,不得不吐槽Bmob的开发文档真的很奇葩,数据服务那篇的Android有详细配置,另一篇的居然没有截个图大家感受一下。
在这里插入图片描述
还有一定要注意的一个问题就是
去配置这个BmobContentProvider

<application>
···
<provider
    android:name="cn.bmob.v3.util.BmobContentProvider"
    android:authorities="你的应用包名.BmobContentProvider">
</provider>
···
</application>

然后这个application又在哪里呢?

在这里插入图片描述

从上图我们就能找到我们需要的了。 然后就是第二个问题出现了,我的5.0系统的安卓手机出现错误提示让我卸载掉

在这里插入图片描述
兼容Android6.0系统所需,如果这句话报错,可在dependencies标签下使用compile 'cn.bmob.android:http-legacy:1.0’
useLibrary ‘org.apache.http.legacy’
根据这个提示看样子我需要把这两段都删掉似乎就可以了。
但是还是无法使用。所以如果5.0系统版本要用的话就只能降SDK的版本去使用才行。不过时间紧凑暂时先放着这个问题6.0以上版本能用即可。尴尬!

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻烟飘荡

我想吃甜筒

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值