加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

创建比目云应用访问云数据库的步骤

(2019-06-05 14:51:25)
标签:

比目云

应用

数据库

bmon

在没有安装sql server ,或者为了方便多方合作调试,可以使用网上比目云数据库的免费部分。

第一部分 创建比目应用

 首先进入比目科技的页面 https://www.bmob.cn
 点击 页面右上角的 【文档】 

创建比目云应用访问云数据库的步骤


点击  数据服务之下 【ndroid】 

创建比目云应用访问云数据库的步骤


进入如下介绍创建应用步骤的网页 http://doc.bmob.cn/data/android/index.html


创建比目云应用访问云数据库的步骤


 
 以下就是按照文档的说明,一步一步来创建应用:


一  进入 https://www.bmob.cn/ 注册账号

     输入邮箱即可注册

二  创建应用,并获得应用ID 

 https://www.bmob.cn/app/list
 点击 左侧列表中 【应用】 -- 输入应用名称textPro -- 选择应用类型 其它 -- 选择开发版(免费)-- 创建应用

创建比目云应用访问云数据库的步骤


三 获取应用密钥

 在应用中点击  应用key --  应用密钥, 复制Application ID,供Android程序使用



四 创建数据库表

 点击 左侧列表中 数据 -- 中间列的 添加表,  创建表 Person

创建比目云应用访问云数据库的步骤


五 创建表列,输入数据

 点击  中间列的 刚创建的表 -- Person
  点击右侧列中的  【添加列】,分别添加字符型的 name 和 address
  点击右侧列中的  【添加行】,分别点击列 name 和 address,输入内容保存即可
 
创建比目云应用访问云数据库的步骤






第二部分 建立对应的Android 应用项目

一 启动Android Studio ,  建立项目 BmobApplication

注意:为了防止博客过滤,以下博文中
将所有的<替换为<<font color="#df06fe">后补一个空格,成为“< ”
将所有的>替换为>加一个空格,成为“ >”

文件夹为 D:\BmobApplication


二 SDK导入

以下文件中的蓝体部分

1 在 Project 的 build.gradle 文件中添加 Bmob的maven仓库地址:

build.gradle(Projects:BmobApplication)

D:\BmobApplication\build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
//Bmob的maven仓库地址--必填
        maven { url "https://raw.github.com/bmob/bmob-android-sdk/master" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

创建比目云应用访问云数据库的步骤



2 在app的build.gradle文件中添加依赖文件:

build.gradle(Module:app)

D:\BmobApplication\app\build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.example.mx.bmobapplication"
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        useLibrary 'org.apache.http.legacy'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation 'cn.bmob.android:bmob-sdk:3.7.3-rc1'
    implementation "io.reactivex.rxjava2:rxjava:2.2.2"
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'com.squareup.okio:okio:2.1.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.okhttp3:okhttp:3.12.0'

}


创建比目云应用访问云数据库的步骤



三 配置AndroidManifest.xml
   
 1 在AndroidManifest.xml文件中添加相应的权限:

 2 配置ContentProvider
  
  参见如下的两处蓝体区域 

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

    < !-- 允许联网 -- >
    < 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" / >
    < !--android:networkSecurityConfig="@xml/network_security_config"-- >

    < application
         android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="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.mx.bmobapplication.BmobContentProvider" >
        < /provider >

        < activity android:name=".BaseActivity" >< /activity >
    < /application >

< /manifest >


四 创建和比目云表对应的类文件 Person.java

D:\BmobApplication\app\src\main\java\com\example\mx\bmobapplication

package com.example.mx.bmobapplication;

import cn.bmob.v3.BmobObject;

public class Person extends BmobObject {
    private String name;
    private String address;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}


五  功能创建于布局文件 activity_main.xml

D:\BmobApplication\app\src\main\res\layout

< ?xml version="1.0" encoding="utf-8"? >
< android.support.constraint.ConstraintLayout 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:id="@+id/tvTestResult2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    < TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" / >

    < Button
        android:id="@+id/btnUpdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="24dp"
        android:text="Update"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" / >

    < Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="24dp"
        android:text="Add"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" / >

    < Button
        android:id="@+id/btnSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="156dp"
        android:text="select"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" / >

    < Button
        android:id="@+id/btnDel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="148dp"
        android:text="del"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" / >

    < TextView
        android:id="@+id/tvTestResult"
        android:layout_width="373dp"
        android:layout_height="198dp"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" / >

    < Button
        android:id="@+id/btnClear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="148dp"
        android:layout_marginTop="224dp"
        android:text="clear"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" / >

< /android.support.constraint.ConstraintLayout >


六  初始化和操作数据表

D:\BmobApplication\app\src\main\java\com\example\mx\bmobapplication

在 MainActivity.java 文件中加入初始化语句

1 在应用程序启动的Application的onCreate()方法中初始化Bmob功能。

   Bmob.initialize(this, "56179fba4b764c6a7b0191b0a42e3146");//Bmob应用ID

2 加入 增加,修改,删除,查询 数据的功能

以下 的蓝体部分

package com.example.mx.bmobapplication;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
public class MainActivity extends Activity {
    private View btnTest;
    private View btnAdd;
    private View btnUpdate;
    private View btnSelect;
    private View btnDel;
    private View btnClear;
    private TextView tvTestResult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bmob.initialize(this, "56179fba4b764c6a7b0191b0a42e3146");//Bmob应用ID

        btnAdd=findViewById(R.id.btnAdd);
        btnUpdate=findViewById(R.id.btnUpdate);
        btnSelect=findViewById(R.id.btnSelect);
        btnDel=findViewById(R.id.btnDel);
        btnClear=findViewById(R.id.btnClear);
        tvTestResult = (TextView)findViewById(R.id.tvTestResult);

        btnAdd.setOnClickListener(getClickEvent());
        btnUpdate.setOnClickListener(getClickEvent());
        btnSelect.setOnClickListener(getClickEvent());
        btnDel.setOnClickListener(getClickEvent());
        btnClear.setOnClickListener(getClickEvent());

    }

    private View.OnClickListener getClickEvent(){
        return new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                test(v);
            }
        };
    }

    private  void test(final View v)
    {
        Runnable run = new Runnable()
        {
            @Override
            public void run()
            {
                switch(v.getId())
                {
                    case R.id.btnAdd://添加数据
                        final Person p2= new Person();
                        p2.setName("lucky3");
                        p2.setAddress("北京海淀3");
                        p2.save(new SaveListener< String >() {
                            @Override
                            public void done(String objectId, BmobException e) {
                                if (e == null) {
                                    tvTestResult.setText("添加数据成功,返回objectId为:" + objectId);
                                } else {
                                    tvTestResult.setText(String.format("创建数据失败:" + e.getMessage() + " "+v.getId() ) );
                                }
                            }
                        });
                        break;

                    case R.id.btnUpdate: //  修改一行数据
                       final Person p22 = new Person();
                        p22.setAddress("雄安新区");
                        p22.update("8b3ea72e24", new UpdateListener() {

                            @Override
                            public void done(BmobException e) {
                                if (e == null) {
                                    tvTestResult.setText("更新成功 :" + p22.getUpdatedAt());
                                } else {
                                     tvTestResult.setText("更新失败:" + e.getMessage());
                                }
                            }

                        });
                        break;

                    case R.id.btnSelect://查询所有数据
                        final     BmobQuery< Person > bmobQuery = new BmobQuery< Person >();
                        BmobQuery< Person > query = new BmobQuery< Person >();
                        query.findObjects( new FindListener< Person >() {
                            @Override
                            public void done(List< Person > object,BmobException e) {
                                if (e == null) {
                                   StringBuilder sb=new StringBuilder();
                                    sb.append("查询成功:共" + object.size() + "条数据。");
                                    for(Person p:object)
                                    {
                                        sb.append( String.format(p.getName() +","+ p.getAddress() ) );
                                    }
                                    tvTestResult.setText(sb);
                                } else {
                                    tvTestResult.setText("查询失败:" + e.getMessage());
                                }
                            }
                        });

                        break;

                    case R.id.btnDel://删除一行数据
                        final Person p24 = new Person();
                        p24.setObjectId("4a8be7a289");
                        p24.delete(new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if(e==null){
                                    tvTestResult.setText("删除成功:"+p24.getUpdatedAt());
                                }else{
                                   // Toast.makeText(getApplicationContext(), "删除失败 :" ,     Toast.LENGTH_SHORT);
                                    tvTestResult.setText("删除失败"+ e.getMessage());
                                }
                            }

                        });
                        break;

                    case R.id.btnClear://清理结果框数据
                        tvTestResult.setText("");
                        break;

                }//switch
            }
        };
        new Thread(run).start();

    }

}


六 执行的结果如下:

创建比目云应用访问云数据库的步骤

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有