Android移动后端服务(BAAS)快速搭建后台服务器之Bmob-android学习之旅(75)
2015-08-25 12:14
633 查看
个人移动开发者的最头疼的问题,就是App的网络后台,包含数据库等,国外目前有比较成熟的解决方案,但是限制于墙的问题,推荐国内的解决方案,比较出名的是Bmob和AVOS cloud和Atom等,这一次我们重点介绍Bmob
使用步骤
1.下载android SDK,里卖包括一个简单的demo和Bmob的两个jar包,可能还有一些Okhttp这类的Jar包,只需要把和Bmob相关的jar包复制到app底下的libs下
2.android studio下需要邮件app项目,打开module setting,选中dependency,电击右上角的加号,选择从文件添加,找到libs下面的jar包,依次加入
3.打开快速开发文档,在manifest里面导入需要的权限,注意javaBean需要继承BmobObjext,这个类封装了网络操作像在本地一样执行存贮然后,在activity里面初始化
4.还有不懂的看demo和开发文档,左边栏是导航很清楚
Bmob的数据库和后台推送,以及短信和app网页这些技术很好,我仅仅使用了数据库的功能
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
下面简要介绍我的demo,是一个和手台数据库交互的登陆系统,包括注册和登陆界面,会有各种的输入检查,包括手机号是否重复,密码和再次输入的密码是否一致,是否为空等,注册后的信息存放在数据库,登陆成功后进入一个空的Activity,显示Toast登陆成功
废话不多说,代码如下
登陆类代码
package com.example.euler_kalvinhe.bmobtest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.List; import cn.bmob.v3.Bmob; import cn.bmob.v3.BmobQuery; import cn.bmob.v3.listener.FindListener; /** * Created by Euler-KalvinHe on 2015/8/24. */ public class LoginActivity extends Activity { private EditText userName,password; private Button login,regeter; String username,pass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); initViews(); Toast.makeText(LoginActivity.this,username+pass,Toast.LENGTH_LONG).show(); Bmob.initialize(this, "79bda8edfbabc323575348b6d50508e6"); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { username = userName.getText().toString(); pass = password.getText().toString(); BmobQuery<User> query = new BmobQuery<User>(); query.addWhereEqualTo("userName",username); query.findObjects(LoginActivity.this,new FindListener<User>() { @Override public void onSuccess(List<User> users) { String str = null; str = users.get(0).getPassword(); if (str.equals(pass)){ Intent intent = new Intent(LoginActivity.this,OutActivity.class); startActivity(intent); }else{ Toast.makeText(LoginActivity.this,"用户名或者密码不正确",Toast.LENGTH_LONG).show(); } // Toast.makeText(LoginActivity.this,str,Toast.LENGTH_LONG).show(); } @Override public void onError(int i, String s) { Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_LONG).show(); } }); } }); regeter.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(LoginActivity.this,RegesterActivity.class); startActivity(intent); finish(); } }); } private void initViews(){ userName = (EditText) findViewById(R.id.userName); login = (Button) findViewById(R.id.log); password = (EditText) findViewById(R.id.password); regeter = (Button) findViewById(R.id.regester); } private void logIn(){ username = userName.getText().toString(); pass = password.getText().toString(); } private String queryPass(String username){ final String[] passTwo = {null}; if (username == null){ return null; } BmobQuery<User> query = new BmobQuery<User>(); query.addWhereEqualTo("userName",username); query.findObjects(LoginActivity.this,new FindListener<User>() { @Override public void onSuccess(List<User> users) { passTwo[0] = users.get(0).getPassword(); Toast.makeText(LoginActivity.this,passTwo[0],Toast.LENGTH_LONG).show(); } @Override public void onError(int i, String s) { Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_LONG).show(); } }); return passTwo[0]; } }
注册类代码
package com.example.euler_kalvinhe.bmobtest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.List; import cn.bmob.v3.Bmob; import cn.bmob.v3.BmobQuery; import cn.bmob.v3.listener.FindListener; import cn.bmob.v3.listener.SaveListener; public class RegesterActivity extends Activity{ private EditText userName, phoneNumber, password, passwordAgain; private Button regester,login; private String username,pass,passAgain; private String phone; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.regester); initViews(); Bmob.initialize(this, "79bda8edfbabc323575348b6d50508e6"); regester.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { regesterUser(); } }); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(RegesterActivity.this,LoginActivity.class); startActivity(intent); finish(); } }); } private void initViews() { regester = (Button) findViewById(R.id.regester); passwordAgain = (EditText) findViewById(R.id.passwordAgain); password = (EditText) findViewById(R.id.password); phoneNumber = (EditText) findViewById(R.id.phoneNumber); userName = (EditText) findViewById(R.id.userName); login = (Button) findViewById(R.id.login); } private void regesterUser(){ username = userName.getText().toString(); pass = password.getText().toString(); passAgain = passwordAgain.getText().toString(); phone = phoneNumber.getText().toString(); if (username != null && pass != null && password != null && phone != null ){ if ( pass.equals(passAgain)){ BmobQuery<User> query = new BmobQuery<User>(); query.findObjects(RegesterActivity.this,new FindListener<User>() { @Override public void onSuccess(List<User> users) { boolean flag = false; for (User u:users){ if (phone.equals(u.getPhoneNumber())){ flag = true; break; } } if (flag){ phoneNumber.setText(""); Toast.makeText(RegesterActivity.this, "手机号已经被注册,请重试", Toast.LENGTH_LONG).show(); }else{ User user = new User(); user.setUserName(username); user.setPassword(pass); user.setPhoneNumber(phone); user.save(RegesterActivity.this,new SaveListener() { @Override public void onSuccess() { Toast.makeText(RegesterActivity.this,"注册成功",Toast.LENGTH_LONG).show(); Intent intent = new Intent(RegesterActivity.this,LoginActivity.class); startActivity(intent); finish(); } @Override public void onFailure(int i, String s) { Toast.makeText(RegesterActivity.this,"注册失败",Toast.LENGTH_LONG).show(); } }); } } @Override public void onError(int i, String s) { Toast.makeText(RegesterActivity.this,"请检查网络重试",Toast.LENGTH_LONG).show(); } });}else{ password.setText(""); passwordAgain.setText(""); Toast.makeText(RegesterActivity.this,"密码不一致,请重试",Toast.LENGTH_LONG).show(); } }else { userName.setText(""); password.setText(""); passwordAgain.setText(""); phoneNumber.setText(""); Toast.makeText(RegesterActivity.this,"不能为空",Toast.LENGTH_LONG).show(); } } }
javaBmob代码
package com.example.euler_kalvinhe.bmobtest; import cn.bmob.v3.BmobObject; /** * Created by Euler-KalvinHe on 2015/8/24. */ public class User extends BmobObject{ private String userName; private String phoneNumber; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
主类代码
package com.example.euler_kalvinhe.bmobtest; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; /** * Created by Euler-KalvinHe on 2015/8/24. */ public class OutActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast.makeText(this,"登陆成功",Toast.LENGTH_LONG).show(); } }
布局代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/userName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名"/> <EditText android:id="@+id/phoneNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="电话号码"/> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码"/> <EditText android:id="@+id/passwordAgain" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="确认密码"/> <Button android:id="@+id/regester" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册" /> <Button android:id="@+id/login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="返回登录" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/userName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名"/> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码"/> <Button android:id="@+id/log" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登陆" /> <Button android:id="@+id/regester" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册" /> </LinearLayout>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
github项目地址,大家可以去下载项目源码
https://github.com/fengsehng/BmobTest
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件