Bmob开发指南【android端】
作为一个开发者,数据的存储,用户的登陆,验证等操作,对程序来说是必不可少的,下面我们将从Bmob的基本信息开始讲起:
【Bmob】
帮助文档:http://www.bmob.cn/site/getdocs
基本操作:http://docs.bmob.cn/android/faststart/index.html?menukey=fast_start&key=start_android
SDK下载: http://www.bmob.cn/site/sdk
【用户的基本操作】
Ⅰ用户注册:
用户注册的表有两种,一种继承BmobUser,一种继承BmobObject,但是大部分作为用户的注册使用的一般是BmobUser
JavaBean代码:
public class Students extends BmobUser { private int age; private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
实现方法:
private void showRegist() { //用户注册的方法 String userName=edt_user.getText().toString(); String password=edt_password.getText().toString(); Students stu=new Students(); stu.setUsername(userName); //Username以及Password为Bmob中的User表,此表字段已经存在,故不可在javaBean中进行创建 stu.setPassword(password); stu.setAge(21); stu.setEmail("[email protected]"); //测试使用,邮箱验证的功能 stu.setAddress("滨州"); /* *用户注册的提交 */ stu.signUp(MainActivity.this,new SaveListener() { @Override public void onSuccess() { Toast.makeText(MainActivity.this,"注册成功",Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int i, String s) { Toast.makeText(MainActivity.this,"注册失败"+s,Toast.LENGTH_SHORT).show(); } }); }后台数据库:
Ⅱ用户登录:
private void showSubmit() { //用户登录的方法 String userName=edt_user.getText().toString(); String password=edt_password.getText().toString(); final BmobUser user=new BmobUser();//创建对象,这个为Bmob创建的类 //与数据库中的信息进行比对,成功返回成功,失败返回失败的信息 user.setUsername(userName); user.setPassword(password); /* *用户登录 */ user.login(MainActivity.this,new SaveListener() { @Override public void onSuccess() { if(user.getEmailVerified()) { Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show(); }else{ //邮箱未验证 } } @Override public void onFailure(int i, String s) { Toast.makeText(MainActivity.this,"登录失败"+s,Toast.LENGTH_SHORT).show(); } }); } }
Ⅲ更新用户数据
private void showUpdate() { //用户更新的方法 Students stu=BmobUser.getCurrentUser(MainActivity.this,Students.class); stu.setAddress("临沂"); stu.setAge(25); stu.update(MainActivity.this,"5c8095e99b",new UpdateListener() { @Override public void onSuccess() { Toast.makeText(MainActivity.this,"更新成功",Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int i, String s) { Toast.makeText(MainActivity.this,"更新失败"+s,Toast.LENGTH_SHORT).show(); } }); }
Ⅳ修改密码
private void showRepassword() { //修改密码的方法 final String email="[email protected]"; BmobUser.resetPassword(MainActivity.this,email,new ResetPasswordListener() { @Override public void onSuccess() { Toast.makeText(MainActivity.this,"修改密码,请到"+email,Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int i, String s) { Toast.makeText(MainActivity.this,"重置密码失败",Toast.LENGTH_SHORT).show(); } }); }
ps:此时系统会默认向你输入的邮箱发送修改密码的邮件
-----------------------------------------------------------------------------------------------------------------------------------
【图片操作】
Ⅴ上传图片
private void submit() { //上传图片 String path="sdcard/Download/shareone.jpg"; final BmobFile file=new BmobFile(new File(path)); //创建BmobFile对象,转换为Bmob对象 file.upload(MainActivity.this,new UploadFileListener() { @Override public void onSuccess() { share share=new share(); share.setName("ShareOne"); share.setAge(20); share.setIcon(file); //设置图片 share.save(MainActivity.this,new SaveListener() { @Override public void onSuccess() { Toast.makeText(MainActivity.this,"上传成功",Toast.LENGTH_LONG).show(); } @Override public void onFailure(int i, String s) { Toast.makeText(MainActivity.this,"上传失败"+s,Toast.LENGTH_LONG).show(); } }); }
javaBean类:
public class share extends BmobObject { private String name; private int age; private BmobFile icon; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public BmobFile getIcon() { return icon; } public void setIcon(BmobFile icon) { this.icon = icon; } }
后台显示:
Ⅵ下载图片
private void download() { //下载图片 BmobQuery<share> query=new BmobQuery<share>(); query.getObject(MainActivity.this,"f4514dcb9d",new GetListener<share>() { @Override public void onSuccess(share share) { BmobFile icon=share.getIcon(); icon.loadImage(MainActivity.this,image); } @Override public void onFailure(int i, String s) { } }); }
Ⅶ加载图片缩略图
private void load() { //加载缩略图 BmobQuery<share> query_load=new BmobQuery<share>(); query_load.getObject(MainActivity.this,"f4514dcb9d",new GetListener<share>() { @Override public void onSuccess(share share) { BmobFile icon=share.getIcon(); icon.loadImageThumbnail(MainActivity.this,load_show,100,100); } @Override public void onFailure(int i, String s) { } }); }
-------------------------------------------------------------------------------------------------------
【数据操作】
JavaBean类:
public class FootBaller extends BmobObject { private String name; private int age; private int score; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
Ⅷ增加数据
private void showCreate() { FootBaller fb=new FootBaller(); fb.setName("monster"); fb.setAge(20); fb.setScore(100); fb.save(MainActivity.this,new SaveListener() { @Override public void onSuccess() { Toast.makeText(MainActivity.this,"创建成功",Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int i, String s) { Toast.makeText(MainActivity.this,"创建失败"+s,Toast.LENGTH_SHORT).show(); } }); }
Ⅸ更新数据
private void update() { FootBaller fb=new FootBaller(); fb.setName("MonsterLin"); fb.setScore(80); fb.setAge(26); fb.update(MainActivity.this, "ed8f8bdd14", new UpdateListener() { @Override public void onSuccess() { Toast.makeText(MainActivity.this, "更新成功", Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int i, String s) { Toast.makeText(MainActivity.this, "创建失败" + s, Toast.LENGTH_SHORT).show(); } }); }
Ⅹ删除数据
private void delete() { FootBaller fb=new FootBaller(); fb.setObjectId("7a01c65384"); //删除对应的ObjectId fb.delete(MainActivity.this,new DeleteListener() { @Override public void onSuccess() { Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int i, String s) { Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show(); } }); }
Ⅺ查询数据
private void query() { BmobQuery<FootBaller> query=new BmobQuery<FootBaller>(); query.addWhereEqualTo("name","monster"); query.findObjects(MainActivity.this,new FindListener<FootBaller>() { @Override public void onSuccess(List<FootBaller> footBallers) { for (FootBaller fb:footBallers) { Toast.makeText(MainActivity.this, "查询数据成功"+fb.getName()+"---"+fb.getScore(), Toast.LENGTH_SHORT).show(); } } @Override public void onError(int i, String s) { Toast.makeText(MainActivity.this, "查询失败"+s, Toast.LENGTH_SHORT).show(); } }); }
【邮箱验证】
邮箱验证参考【用户注册】板块,邮箱验证需要在你对应的Bmob下开启邮箱验证的服务
------------------------------------------------------------------------------------------------------
【小编有话说】
1.Bmob的初始化建议:
public class BaseActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // 初始化BmobSDK Bmob.initialize(this, "Application ID");
然后书写的类继承BaseActivity
2.注意BmobUser与BmobObject的使用:BmobUser的使用的时候,由于这张表作为系统表,所以已经存在的字段不允许自己重复定义,否则会出现编译不通过;BmobObject的表用户可以自定任意定义字段
3.在使用Bmob的使用需要加载SDK以及配置清单文件,具体操作:http://docs.bmob.cn/android/faststart/index.html?menukey=fast_start&key=start_android#index_%E5%AE%89%E8%A3%85BmobSDK
4.后续持续跟新…..
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。