其他分享
首页 > 其他分享> > Bmob后端云的使用

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

模块结构图:

 

 

manifests:
 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 }

 

标签:Toast,后端,show,Bmob,MainActivity,Person,使用,import,public
来源: https://www.cnblogs.com/hemeiwolong/p/12874714.html