dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'io.github.bmob:android-sdk:3.8.4'
implementation "io.reactivex.rxjava2:rxjava:2.2.8"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'com.squareup.okhttp3:okhttp:4.8.1'
implementation 'com.squareup.okio:okio:2.2.2'
implementation 'com.google.code.gson:gson:2.8.5'
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo">
<application
android:name=".DemoApplication"
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/Theme.Demo">
<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.demo.BmobContentProvider">
provider>
application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
manifest>
package com.example.demo;
import android.app.Application;
import android.util.Log;
import cn.bmob.v3.Bmob;
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Log.e("DemoApplication", "onCreate");
Bmob.initialize(this, "***********************");
}
}
<LinearLayout 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"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加一个人"
android:id="@+id/btn_add_person"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取一条数据"
android:id="@+id/btn_get_person"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改一条数据"
android:id="@+id/btn_update_person"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除一条数据"
android:id="@+id/btn_del_person"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询多条数据"
android:id="@+id/btn_query_person"/>
LinearLayout>
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.List;
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 AppCompatActivity {
private Button btn_add_person;
private Button btn_get_person;
private Button btn_update_person;
private Button btn_del_person;
private Button btn_query_person;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_query_person = findViewById(R.id.btn_query_person);
btn_query_person.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BmobQuery<student> studentBmobQuery = new BmobQuery<>();
// 等于
// studentBmobQuery.addWhereEqualTo("age", 20);
// studentBmobQuery.addWhereNotEqualTo("name", "曹阳");
// 小于
// studentBmobQuery.addWhereLessThan("age", 20);
studentBmobQuery.addWhereLessThanOrEqualTo("age", 20);
studentBmobQuery.findObjects(new FindListener<student>() {
@Override
public void done(List<student> list, BmobException e) {
if(e == null){
Toast.makeText(MainActivity.this,"查询成功 " ,
Toast.LENGTH_SHORT).show();
for(student s : list){
Log.e("findObjects", s.toString());
}
}else{
Toast.makeText(MainActivity.this,"查询失败 e="+ e.toString(),
Toast.LENGTH_SHORT).show();
}
}
});
}
});
btn_del_person = findViewById(R.id.btn_del_person);
btn_del_person.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//bb09655c7d
student s = new student();
s.delete("bb09655c7d", new UpdateListener() {
@Override
public void done(BmobException e) {
if(e == null){
Toast.makeText(MainActivity.this,"删除成功 " ,
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"删除失败 e="+ e.toString(),
Toast.LENGTH_SHORT).show();
}
}
});
}
});
btn_update_person = findViewById(R.id.btn_update_person);
btn_update_person.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
student s = new student();
s.setAddress("广州塔");
s.setAge(30);
s.update("0a7172c986", new UpdateListener() {
@Override
public void done(BmobException e) {
if(e == null){
Toast.makeText(MainActivity.this,"更新成功 " ,
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"更新失败 e="+ e.toString(),
Toast.LENGTH_SHORT).show();
}
}
});
}
});
btn_get_person = findViewById(R.id.btn_get_person);
btn_get_person.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BmobQuery<student> personBmobQuery = new BmobQuery<>();
personBmobQuery.getObject("0a7172c986", new QueryListener<student>() {
@Override
public void done(student s, BmobException e) {
if(e == null){
Toast.makeText(MainActivity.this,"查询成功 "+ s.toString(),
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"查询失败 e="+ e.toString(),
Toast.LENGTH_SHORT).show();
}
}
});
}
});
btn_add_person = findViewById(R.id.btn_add_person);
btn_add_person.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// addPerson();
addStudent();
}
});
}
private void addStudent() {
student s = new student();
s.setAge(20);
s.setName("曹阳");
s.setClass_name("19软件4班");
s.setAddress("广州市天源路789号");
s.save(new SaveListener<String>() {
@Override
public void done(String s, BmobException e) {
if(e == null){
Toast.makeText(MainActivity.this,"保存成功 s="+ s,
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"保存失败 e="+ e.toString(),
Toast.LENGTH_SHORT).show();
}
}
});
}
private void addPerson() {
Person person = new Person();
person.setAge(20);
person.setName("刘威");
person.save(new SaveListener<String>() {
@Override
public void done(String s, BmobException e) {
if(e == null){
Toast.makeText(MainActivity.this,"保存成功 s="+ s,
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"保存失败 e="+ e.toString(),
Toast.LENGTH_SHORT).show();
}
}
});
}
}
package com.example.demo;
import cn.bmob.v3.BmobObject;
public class Person extends BmobObject {
String name;
Number age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Number getAge() {
return age;
}
public void setAge(Number age) {
this.age = age;
}
}
package com.example.demo;
import cn.bmob.v3.BmobObject;
public class student extends BmobObject {
String name;
Number age;
String class_name;
String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Number getAge() {
return age;
}
public void setAge(Number age) {
this.age = age;
}
public String getClass_name() {
return class_name;
}
public void setClass_name(String class_name) {
this.class_name = class_name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
1、创建MyUser类
package com.example.demo;
import cn.bmob.v3.BmobUser;
public class MyUser extends BmobUser {
String name;
Number age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Number getAge() {
return age;
}
public void setAge(Number age) {
this.age = age;
}
@Override
public String toString() {
return "MyUser{" +
"usernmae = " + getUsername() +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
2.在主界面中进行测试
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
public class MainActivity extends AppCompatActivity {
private Button btn_reg;
private Button btn_login;
private Button btn_logout;
private Button btn_get_user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//第一:默认初始化
Bmob.initialize(this, "***********************");
btn_login = findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyUser myUser = new MyUser();
myUser.setUsername("zhangsan");
myUser.setPassword("zhangsan");
myUser.login(new SaveListener<MyUser>() {
@Override
public void done(MyUser myUser, BmobException e) {
if(e == null){
Log.e("Bmob","登录成功 " + myUser.toString());
}else{
Log.e("Bmob","登录失败 " + e.toString());
}
}
});
}
});
btn_get_user = findViewById(R.id.btn_get_user);
btn_get_user.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
if(myUser == null){
Toast.makeText(MainActivity.this, "您还未登录",
Toast.LENGTH_SHORT).show();
}else {
Log.e("Bmob", "myUser = " + myUser.toString());
}
}
});
btn_logout = findViewById(R.id.btn_logout);
btn_logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BmobUser.logOut();
}
});
btn_reg = findViewById(R.id.btn_reg);
btn_reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyUser myUser = new MyUser();
myUser.setUsername("zhangsan");
myUser.setPassword("zhangsan");
myUser.setName("张三");
myUser.setAge(20);
myUser.signUp(new SaveListener<MyUser>() {
@Override
public void done(MyUser myUser, BmobException e) {
if(e == null){
Log.e("Bmob","注册成功 " + myUser.toString());
}else{
Log.e("Bmob","注册失败 " + e.toString());
}
}
});
}
});
}
}