Bmob后端云的使用

参考链接:http://doc.bmob.cn/data/android/#sdk_1

如果报错Error: Invoke-customs are only supported starting with Android O (--min-api 26):https://blog.csdn.net/Test_tju/article/details/100516933

模块结构图:

 

工程的build.gradle:

 1 import jdk.internal.dynalink.linker.LinkerServices
 2 
 3 // Top-level build file where you can add configuration options common to all sub-projects/modules.
 4 
 5 buildscript {
 6     
 7     repositories {
 8         google()
 9         jcenter()
10     }
11     dependencies {
12         classpath 'com.android.tools.build:gradle:3.2.1'
13 
14         // NOTE: Do not place your application dependencies here; they belong
15         // in the individual module build.gradle files
16     }
17 }
18 
19 allprojects {
20     repositories {
21         google()
22         jcenter()
23         //Bmob的maven仓库地址--必填
24         maven {url 'https://dl.bintray.com/chaozhouzhang/maven' }
25     }
26 }
27 
28 task clean(type: Delete) {
29     delete rootProject.buildDir
30 }

 

 

 模块的build.gradle:android{}里添加useLibrary.......,dependencies {}里添加多个implementation 

 1 apply plugin: 'com.android.application'
 2 
 3 android {
 4     compileSdkVersion 29
 5 
 6     useLibrary 'org.apache.http.legacy'
 7 
 8     defaultConfig {
 9         applicationId "com.mingrisoft.bmobtest2"
10         minSdkVersion 15
11         targetSdkVersion 29
12         versionCode 1
13         versionName "1.0"
14 
15         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16 
17     }
18 
19     buildTypes {
20         release {
21             minifyEnabled false
22             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23         }
24     }
25 
26     compileOptions {
27         sourceCompatibility JavaVersion.VERSION_1_8
28         targetCompatibility JavaVersion.VERSION_1_8
29     }
30 
31 }
32 
33 dependencies {
34     implementation fileTree(dir: 'libs', include: ['*.jar'])
35 
36     implementation 'com.android.support:appcompat-v7:+'
37     implementation 'com.android.support.constraint:constraint-layout:1.1.3'
38     testImplementation 'junit:junit:4.12'
39     androidTestImplementation 'com.android.support.test:runner:1.0.2'
40     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
41 
42     implementation 'cn.bmob.android:bmob-sdk:3.7.8'
43     implementation "io.reactivex.rxjava2:rxjava:2.2.8"
44     implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
45     implementation 'com.squareup.okhttp3:okhttp:3.14.1'
46     implementation 'com.squareup.okio:okio:2.2.2'
47     implementation 'com.google.code.gson:gson:2.8.5'
48 }

 

 

manifests:添加权限和provider
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.mingrisoft.bmobtest">
 4     <!--允许联网 -->
 5     <uses-permission android:name="android.permission.INTERNET" />
 6     <!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息  -->
 7     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 8     <!--获取wifi网络状态的信息 -->
 9     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
10     <!--保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
11     <uses-permission android:name="android.permission.WAKE_LOCK" />
12     <!--获取sd卡写的权限,用于文件上传和下载-->
13     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
14     <!--允许读取手机状态 用于创建BmobInstallation-->
15     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
16 
17     <application
18         android:allowBackup="true"
19         android:icon="@mipmap/ic_launcher"
20         android:label="@string/app_name"
21         android:roundIcon="@mipmap/ic_launcher_round"
22         android:supportsRtl="true"
23         android:theme="@style/AppTheme">
24         <activity android:name=".MainActivity">
25             <intent-filter>
26                 <action android:name="android.intent.action.MAIN" />
27 
28                 <category android:name="android.intent.category.LAUNCHER" />
29             </intent-filter>
30         </activity>
31 
32         <provider
33             android:name="cn.bmob.v3.util.BmobContentProvider"
34             android:authorities="com.mingrisoft.bmobtest.BmobContentProvider"> //这里要改为自己的包名
35         </provider>
36     </application>
37 
38 </manifest>

 

activity_main.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <TextView
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:text="Hello World!"
13         app:layout_constraintBottom_toBottomOf="parent"
14         app:layout_constraintLeft_toLeftOf="parent"
15         app:layout_constraintRight_toRightOf="parent"
16         app:layout_constraintTop_toTopOf="parent" />
17 
18 </android.support.constraint.ConstraintLayout>

 

MainActivity:
 1 package com.mingrisoft.bmobtest;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.widget.Toast;
 6 
 7 import cn.bmob.v3.Bmob;
 8 import cn.bmob.v3.BmobQuery;
 9 import cn.bmob.v3.exception.BmobException;
10 import cn.bmob.v3.listener.QueryListener;
11 import cn.bmob.v3.listener.SaveListener;
12 import cn.bmob.v3.listener.UpdateListener;
13 
14 public class MainActivity extends AppCompatActivity {
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20 
21         //第一:默认初始化
22         Bmob.initialize(this, "1368b7fd0a523efab971eb807e1b9bc2");  //这里要设置成自己的key
23 
24         Person p2 = new Person();
25         p2.setName("lucky");
26         p2.setAddress("北京海淀");
27         p2.save(new SaveListener<String>() {
28             @Override
29             public void done(String objectId,BmobException e) {
30                 if(e==null){
31                     Toast.makeText(MainActivity.this, "添加数据成功,返回objectId为:"+objectId, Toast.LENGTH_LONG).show();
32                 }else{
33                     Toast.makeText(MainActivity.this, "创建数据失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
34                 }
35             }
36         });
37 
38         //查找Person表里面id为6b6c11c537的数据
39         BmobQuery<Person> bmobQuery = new BmobQuery<Person>();
40         bmobQuery.getObject("b7ca30c826", new QueryListener<Person>() {
41             @Override
42             public void done(Person object,BmobException e) {
43                 if(e==null){
44                     Toast.makeText(MainActivity.this, "查询成功", Toast.LENGTH_LONG).show();
45                 }else{
46                     Toast.makeText(MainActivity.this, "查询失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
47                 }
48             }
49         });
50 
51         //更新Person表里面id为b80c3fefdb的数据,address内容更新为“北京朝阳”
52         Person p3 = new Person();
53         p3.setAddress("广西南宁");
54         p3.update("b80c3fefdb", new UpdateListener() {
55 
56             @Override
57             public void done(BmobException e) {
58                 if(e==null){
59                     Toast.makeText(MainActivity.this,"更新成功:"+p3.getUpdatedAt(), Toast.LENGTH_LONG).show();
60                 }else{ Toast.makeText(MainActivity.this, "更新失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
61                 }
62             }
63 
64         });
65 
66         Person p4 = new Person();
67         p4.setObjectId("b80c3fefdb");
68         p4.delete(new UpdateListener() {
69 
70             @Override
71             public void done(BmobException e) {
72                 if(e==null){
73                     Toast.makeText(MainActivity.this,"删除成功:"+p4.getUpdatedAt(), Toast.LENGTH_LONG).show();
74                 }else{
75                     Toast.makeText(MainActivity.this,"删除失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
76                 }
77             }
78 
79         });
80     }
81 }

 

Person:
 1 package com.mingrisoft.bmobtest;
 2 
 3 import cn.bmob.v3.BmobObject;
 4 
 5 /** ����javaBean
 6   * @ClassName: Person
 7   * @Description: TODO
 8   * @author smile
 9   * @date 2014-5-20 ����4:12:55
10   */
11 public class Person extends BmobObject {
12     
13     private String name;
14     private String address;
15 
16     public String getName() {
17         return name;
18     }
19     public void setName(String name) {
20         this.name = name;
21     }
22     public String getAddress() {
23         return address;
24     }
25     public void setAddress(String address) {
26         this.address = address;
27     }
28 }

 

云端结果图:

 

posted @ 2020-05-12 10:45  hemeiwolong  阅读(1125)  评论(0编辑  收藏  举报