天天看点

Bmob移动后端云服务平台--Android实践(2)

一、准备工作

1.注册Bmob账号

在网址栏输入www.bmob.cn或者在百度输入Bmob进行搜索,打开Bmob官网后,点击右上角的“注册”,在跳转页面填入你的姓名、邮箱、设置密码,确认后到你的邮箱激活Bmob账户,你就可以用Bmob轻松开发应用了。

Bmob移动后端云服务平台--Android实践(2)

2.网站后台创建应用

登录账号进入bmob后台后,点击后台界面左上角“创建应用”,在弹出框输入你应用的名称,然后确认,你就拥有了一个等待开发的应用。

Bmob移动后端云服务平台--Android实践(2)

3.获取应用密钥和下载SDK

选择你要开发的应用,点击该应用下方对应的“应用密钥”

Bmob移动后端云服务平台--Android实践(2)

获取Application ID后,下载SDK,开发者可以根据自己的需求选择相应的iOS SDK 或Android SDK,点击下载即可。

Bmob移动后端云服务平台--Android实践(2)

不过我一般喜欢在线配置,所以我就没下载这个。

二、代码实现

1、在project的grade添加一下语句

Bmob移动后端云服务平台--Android实践(2)

在app的grade条件以下语句

Bmob移动后端云服务平台--Android实践(2)
Bmob移动后端云服务平台--Android实践(2)

2、在清单文件中添加如下的权限

Bmob移动后端云服务平台--Android实践(2)
Bmob移动后端云服务平台--Android实践(2)

3、初始化BmobSDK

在你应用程序启动的Activity的onCreate()方法中初始化Bmob功能。代码如下所示:

Bmob移动后端云服务平台--Android实践(2)

4、创建继承于BmobObject的实体UserBean

package com.example.wzh.bmobtest2.bean;

import cn.bmob.v3.BmobObject;

/**
 * Author by wzh,Date on 2019/3/17.
 * PS: Not easy to write code, please indicate.
 * 类描述:    继承BmobObject的用户实体类
 */
public class UserBean extends BmobObject {

    private static final long serialVersionUID = 1L;
    private String loginId;
    private String userName;
    private String password;

    public String getLoginId() {
        return loginId;
    }

    public void setLoginId(String loginId) {
        this.loginId = loginId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserBean [loginId=" + loginId + ", userName=" + userName
                + ", password=" + password + "]";
    }
}
           

5、案例效果

Bmob移动后端云服务平台--Android实践(2)

Bmob平台服务器数据

Bmob移动后端云服务平台--Android实践(2)

6、相关的布局文件和清单文件

1.清单文件,AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.wzh.bmobtest2">


    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_LOGS"
        tools:ignore="ProtectedPermissions" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            android:name="cn.bmob.v3.util.BmobContentProvider"
            android:authorities="com.example.wzh.bmobtest2.BmobContentProvider">
        </provider>

    </application>

</manifest>
           

2.主布局文件,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=".MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:text="用户名:"
                android:textColor="#FF0000" />

            <EditText
                android:id="@+id/et_login_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:text="密码:"
                android:textColor="#FF0000" />

            <EditText
                android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <Button
            android:id="@+id/btn_register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="注册"
            android:textColor="#0000FF" />

        <Button
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="登陆"
            android:textColor="#0000FF" />

</LinearLayout>
           

主界面代码MainActivity.java

package com.example.wzh.bmobtest2;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.example.wzh.bmobtest2.bean.UserBean;
import com.example.wzh.bmobtest2.util.ToastUtils;

import java.util.List;

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

import static com.example.wzh.bmobtest2.R.id.btn_register;

public class MainActivity extends Activity implements OnClickListener {

    // Bmob应用创建建时获取的Application id,根据自己创建的应用来写入
    private static final String BMOB_APPLICATION_ID = "812b06a63357a00af19cfb0d5afbe1e0";
    Button btn_register, btn_login;
    EditText et_login_name, et_password;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // 初始化 Bmob SDK
        // 使用时请将第二个参数Application ID替换成你在Bmob服务器端创建的Application ID
        Bmob.initialize(this, BMOB_APPLICATION_ID);
        initView();
    }

    /**
     *
     * 初始化控件
     */
    private void initView() {
        btn_register = (Button) this.findViewById(R.id.btn_register);
        btn_login = (Button) this.findViewById(R.id.btn_login);
        et_login_name = (EditText) this.findViewById(R.id.et_login_name);
        et_password = (EditText) this.findViewById(R.id.et_password);

        btn_register.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                register();
                break;
            case R.id.btn_login:
                login();
                break;
        }
    }

    private void register() {

        String loginId = et_login_name.getText().toString();
        String password = et_password.getText().toString();
        if (loginId.isEmpty() || password.isEmpty()) {
            ToastUtils.toast(this, "密码或账号不为空!");
            return;
        }

        final UserBean us = new UserBean();
        us.setLoginId(loginId);
        us.setPassword(password);
        us.setUserName("Bmob");
        /**
         * 保存数据到Bmob服务器
         */
        us.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if(e==null){
                    ToastUtils.toast(MainActivity.this, us.toString()
                            + " 注册成功");
                }else{
                    int arg0 = 0;
                    String arg1 = null;
                    ToastUtils.toast(MainActivity.this, arg0 + "," + arg1 + " 注册失败");
                }
            }
        });

    }


    private void login() {

        String loginId = et_login_name.getText().toString();
        String password = et_password.getText().toString();
        if (loginId.isEmpty() || password.isEmpty()) {
            ToastUtils.toast(this, "密码或账号不为空!");
            return;
        }

        BmobQuery<UserBean> userQuery = new BmobQuery<UserBean>();

        // 查询条件
        userQuery.addWhereEqualTo("loginId", loginId);
        userQuery.addWhereEqualTo("password", password);

        userQuery.findObjects(new FindListener<UserBean>() {

            @Override
            public void done(List<UserBean> list, BmobException e) {
                if (list != null && list.size() > 0)
                    ToastUtils.toast(MainActivity.this, " 登陆成功");
                else {
                    int arg0 = 0;
                    String arg1 = null;
                    ToastUtils.toast(MainActivity.this, arg0 + "," + arg1 + " 登陆失败");
                }
            }
        });

    }

}

           

实体类UserBean

package com.example.wzh.bmobtest2.bean;

import cn.bmob.v3.BmobObject;

/**
 * Author by wzh,Date on 2019/3/17.
 * PS: Not easy to write code, please indicate.
 * 类描述:    继承BmobObject的用户实体类
 */
public class UserBean extends BmobObject {

    private static final long serialVersionUID = 1L;
    private String loginId;
    private String userName;
    private String password;

    public String getLoginId() {
        return loginId;
    }

    public void setLoginId(String loginId) {
        this.loginId = loginId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserBean [loginId=" + loginId + ", userName=" + userName
                + ", password=" + password + "]";
    }
}

           

工具类ToastUtils.java

package com.example.wzh.bmobtest2.util;

import android.content.Context;
import android.widget.Toast;

/**
 * Author by wzh,Date on 2019/3/17.
 * PS: Not easy to write code, please indicate.
 * Toast提示工具类
 */
public class ToastUtils {

    public static void toast(Context context, String msg){
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    public static void toast(Context context,int msgId){
        Toast.makeText(context, msgId, Toast.LENGTH_SHORT).show();
    }

}