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:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mingrisoft.bmobtest"> <!--允许联网 --> <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" /> <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.mingrisoft.bmobtest.BmobContentProvider"> //这里要改为自己的包名 </provider> </application> </manifest>
activity_main.xml:
<?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: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" /> </android.support.constraint.ConstraintLayout>
MainActivity:
package com.mingrisoft.bmobtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import cn.bmob.v3.Bmob; import cn.bmob.v3.BmobQuery; import cn.bmob.v3.exception.BmobException; import cn.bmob.v3.listener.QueryListener; import cn.bmob.v3.listener.SaveListener; import cn.bmob.v3.listener.UpdateListener; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //第一:默认初始化 Bmob.initialize(this, "1368b7fd0a523efab971eb807e1b9bc2"); //这里要设置成自己的key Person p2 = new Person(); p2.setName("lucky"); p2.setAddress("北京海淀"); p2.save(new SaveListener<String>() { @Override public void done(String objectId,BmobException e) { if(e==null){ Toast.makeText(MainActivity.this, "添加数据成功,返回objectId为:"+objectId, Toast.LENGTH_LONG).show(); }else{ Toast.makeText(MainActivity.this, "创建数据失败:" + e.getMessage(), Toast.LENGTH_LONG).show(); } } }); //查找Person表里面id为6b6c11c537的数据 BmobQuery<Person> bmobQuery = new BmobQuery<Person>(); bmobQuery.getObject("b7ca30c826", new QueryListener<Person>() { @Override public void done(Person object,BmobException e) { if(e==null){ Toast.makeText(MainActivity.this, "查询成功", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(MainActivity.this, "查询失败:" + e.getMessage(), Toast.LENGTH_LONG).show(); } } }); //更新Person表里面id为b80c3fefdb的数据,address内容更新为“北京朝阳” Person p3 = new Person(); p3.setAddress("广西南宁"); p3.update("b80c3fefdb", new UpdateListener() { @Override public void done(BmobException e) { if(e==null){ Toast.makeText(MainActivity.this,"更新成功:"+p3.getUpdatedAt(), Toast.LENGTH_LONG).show(); }else{ Toast.makeText(MainActivity.this, "更新失败:" + e.getMessage(), Toast.LENGTH_LONG).show(); } } }); Person p4 = new Person(); p4.setObjectId("b80c3fefdb"); p4.delete(new UpdateListener() { @Override public void done(BmobException e) { if(e==null){ Toast.makeText(MainActivity.this,"删除成功:"+p4.getUpdatedAt(), Toast.LENGTH_LONG).show(); }else{ Toast.makeText(MainActivity.this,"删除失败:" + e.getMessage(), Toast.LENGTH_LONG).show(); } } }); } }
Person:
package com.mingrisoft.bmobtest; import cn.bmob.v3.BmobObject; /** ????javaBean * @ClassName: Person * @Description: TODO * @author smile * @date 2014-5-20 ????4:12:55 */ 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; } }