基于Bmob平台的账户登录、文件上传及下载逻辑的实现

最近有时间研究了一下Bmob平台的后台数据处理,确实是为应用开发者省去了许多麻烦,让不懂服务器开发的也可以做出棒棒的网络交互应用。

好记性不如烂笔头啊,代码备份一下~


BmobUser实现用户登录和注册:

注册和登录不需要自定义数据表,后台默认—User数据表来保存账户信息(用户名、密码、邮箱、手机)

登录逻辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private  void  login() {
         final  ProgressDialog progress =  new  ProgressDialog(MainActivity. this );
         progress.setMessage( "登录ing>->->" );
         progress.setCanceledOnTouchOutside( false );
         progress.show();
         BmobUser.loginByAccount( this "account" "password" new  LogInListener() {
             @Override
             public  void  done(Users users, BmobException e) {
                 progress.dismiss();
                 if  (e !=  null ){
                     Toast.makeText(MainActivity. this ,
                             "登录失败:code=" +e.getErrorCode()+
                             ",错误描述:" +e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
                 } else {
                     Toast.makeText(MainActivity. this , "登录成功!" , Toast.LENGTH_LONG).show();
                 }
             }
         });
     }
注册逻辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
private  void  registerUser() {
         String account = et_account.getText().toString();
         String password = et_password.getText().toString();
         String confirmpassword = et_confirmpwd.getText().toString();
         if (TextUtils.isEmpty(account)){
             Toast.makeText(RegisterActivity. this "用户名不可为空" , Toast.LENGTH_SHORT).show();
             return ;
         }
         if (TextUtils.isEmpty(password)){
             Toast.makeText(RegisterActivity. this "密码不可以为空" , Toast.LENGTH_SHORT).show();
             return ;
         }
         if (!password.equals(confirmpassword)){
             Toast.makeText(RegisterActivity. this "密码不相同" , Toast.LENGTH_SHORT).show();
             return ;
         }
         final  ProgressDialog progress =  new  ProgressDialog(RegisterActivity. this );
         progress.setMessage( "注册ing>->->" );
         progress.setCanceledOnTouchOutside( false );
         progress.show();
         final  Users user =  new  Users();
         user.setUsername(account);
         user.setPassword(password);
         user.signUp( this new  SaveListener() {
             @Override
             public  void  onSuccess() {
                 progress.dismiss();
                 Toast.makeText(RegisterActivity. this , "成功啦" ,Toast.LENGTH_LONG).show();
                 Intent intent =  new  Intent(RegisterActivity. this ,MainActivity. class );
                 intent.putExtra( "from" , "login" );
                 startActivity(intent);
                 finish();
             }
 
             @Override
             public  void  onFailure( int  i, String s) {
                 Toast.makeText(RegisterActivity. this ,
                         "注册失败:code=" +i+ ",错误描述:" +s,Toast.LENGTH_LONG).show();
             }
         });
 
 
     }

文件上传及下载实现:

其中上传的文件可以在Bmob控制台-文件选项下进行查看,上传和下载的重点是:上传完成后在onSuccess中会返回所上传文件的url,下载时同样也是需要传入文件的下载地址url。一般在同时实现上传文件和其他类型数据时是先实现文件上传然后在在onSuccess中继续实现数据上传的逻辑,最好是在上传数据时将返回的url一起保存。

文件上传逻辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private  void  upload() {  
         File f =  new  File(Environment.getExternalStorageDirectory()+ "/image/test.jpg" );
         final  BmobFile file =  new  BmobFile(f);
         file.upload(MainActivity. this new  UploadFileListener() {  
               
             @Override  
             public  void  onSuccess() {  
                 // TODO Auto-generated method stub  
                 url = file.getUrl();
                 Toast.makeText(MainActivity. this "上传成功" , Toast.LENGTH_SHORT).show();  
             }  
 
             @Override  
             public  void  onFailure( int  arg0, String arg1) {  
                 // TODO Auto-generated method stub  
                 Toast.makeText(MainActivity. this "上传失败" +arg1, Toast.LENGTH_SHORT).show();                        
             }
          });  
     }
 
文件下载逻辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
private  void  downLoad(){
         new  Thread( new  Runnable() {  
             @Override  
             public  void  run() {  
                 // TODO Auto-generated method stub  
                 Looper.prepare();  
                  try  {  
                     Bitmap bitmap1= null ;  
                     URL myUrl;  
                     myUrl =  new  URL(url);  
                     HttpURLConnection conn=(HttpURLConnection)myUrl.openConnection();  
                     conn.setConnectTimeout( 5000 );  
                     conn.connect();  
                     InputStream is=conn.getInputStream();  
                     bitmap1=BitmapFactory.decodeStream(is);  
                     //把bitmap转成圆形  
                     BitmapUtil bmuUtil =  new  BitmapUtil(MainActivity. this );  
                     mBitmap = bmuUtil.toRoundBitmap(bitmap1);  
                     is.close();  
                     Message msg = mhandler.obtainMessage( 1 , mBitmap);  
                     mhandler.sendMessage(msg);  
                 catch  (MalformedURLException e) {  
                         // TODO Auto-generated catch block  
                         e.printStackTrace();  
                 catch  (IOException e) {  
                         // TODO Auto-generated catch block  
                         e.printStackTrace();  
                 }  
                  Looper.loop();  
             }  
         }).start();  
     }

你可能感兴趣的:(Android开发基础)