未加星标

用到BMOB数据库和登录 自己封装的几个方法

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

package com.douwen.kys_36.bmob_im.Util;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.douwen.kys_36.bmob_im.JavaBean.ActiveBean;
import com.douwen.kys_36.bmob_im.JavaBean.PersonBean;
import com.douwen.kys_36.bmob_im.JavaBean.SociationBean;
import com.douwen.kys_36.bmob_im.code;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;
/**
* Created by kys-36 on 2016/10/4.
*/
public class BmobUtil {
/*需要返回值时调用的Handler*/
public static Handler bmobHandler;
/*登录返回 boolean*/
public static final int BMOB_LOGIN_OK = 1;
/*注册返回 boolean*/
public static final int BMOB_REGIST_OK = 0;
/*查询返回 List*/
public static final int BMOB_QUERY_OK = 2;
/*上传头像返回 boolean*/
public static final int BMOB_ICON_OK = 3;
/*修改返回 boolean*/
public static final int BMOB_EDIT_OK = 4;
/*增加JAVABEAN返回*/
public static final int BMOB_SAVE_OK = 5;
/*当前环境*/
private Context mContext;
/*Log提示信息头*/
private final String TAG = "Bmob";
/*上传的头像*/
private String icon;
/*登陆成功判断*/
private boolean islogin = false;
/*注册成功判断*/
private boolean isregist = false;
public BmobUtil(Context context) {
mContext = context;
Bmob.initialize(mContext, code.AppKey);
}
/**
* @param username 用户名
* @param password 密码
* @function 登录
*/
public void bmobLogin(final String username, final String password) {
final Message message = new Message();
message.what = BMOB_LOGIN_OK;
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {Toast.makeText(mContext, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
} else {BmobUser bmobUser = new BmobUser();bmobUser.setUsername(username);bmobUser.setPassword(password);bmobUser.login(mContext, new SaveListener() { @Override public void onSuccess() { islogin = true; Log.e(TAG, "denglu"); EMClient.getInstance().login(username, password, new EMCallBack() {//回调 @Override public void onSuccess() { EMClient.getInstance().groupManager().loadAllGroups(); EMClient.getInstance().chatManager().loadAllConversations(); message.obj = true; bmobHandler.sendMessage(message); } @Override public void onProgress(int progress, String status) { } @Override public void onError(int code, String msg) { Log.e(TAG,"huanxin"+code+" "+msg); message.obj = false; bmobHandler.sendMessage(message); } }); } @Override public void onFailure(int i, String s) { Log.e(TAG, "Login" + i + "&&" + s); message.obj = false; bmobHandler.sendMessage(message); }});Log.e(TAG, "denglucheng");
}
}
/**
* @param username 用户名
* @param password 密码
* @param name 昵称
* @param phoneNumber 手机号码
* @param school 学校
* @function 注册
*/
public void bmobRegist(final String username, final String password, String name, String sex, String phoneNumber, String school) {
final Message message = new Message();
message.what = BMOB_REGIST_OK;
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(school)) {Toast.makeText(mContext, "信息不能为空", Toast.LENGTH_SHORT).show();
} else if (!StringUtil.isMobileNO(phoneNumber)) {Toast.makeText(mContext, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
} else {PersonBean bmobUser = new PersonBean();bmobUser.setUsername(username);bmobUser.setPassword(password);bmobUser.setMobilePhoneNumber(phoneNumber);bmobUser.setName(name);bmobUser.setSchool(school);bmobUser.setSex(sex);bmobUser.signUp(mContext, new SaveListener() { @Override public void onSuccess() { //注册失败会抛出HyphenateException try { EMClient.getInstance().createAccount(username, password);//同步方法 } catch (HyphenateException e) { e.printStackTrace(); } message.obj = true; bmobHandler.sendMessage(message); } @Override public void onFailure(int i, String s) { Log.e(TAG, " " + i + " " + s); message.obj = false; bmobHandler.sendMessage(message); }});
}
}
/**
* @param t JAVABEAN
* @param 泛型,这里可以使用JAVABEAN来让BMOB知道查询的具体类别(每个JavaBean的查询类别在JAVABEAN内部)
* @function 保存进数据库
*/
public void saveMSG(T t){
final Message message = new Message();
message.what = BMOB_SAVE_OK;
t.save(mContext, new SaveListener() [email protected] void onSuccess() { Log.i(TAG,"保存成功"); message.obj = true; bmobHandler.sendMessage(message);[email protected] void onFailure(int i, String s) { Log.e(TAG,"保存失败"); message.obj = false; bmobHandler.sendMessage(message);}
});
}
/**
* @param file 文件路径(图片)
* @param personBean 用户JAVABEAN
* @function 上传头像
*/
public void upLoadIcon(final PersonBean personBean, File file){
final Message message = new Message();
message.what = BMOB_ICON_OK;
if (!StringUtil.isEmpty(personBean.toString())) {if (StringUtil.fileIsExists(file)) { final BmobFile bmobFile = new BmobFile(file); bmobFile.upload(mContext, new UploadFileListener() { @Override public void onSuccess() { icon = bmobFile.getFileUrl(mContext); Log.e(TAG, "上传地址" + icon); personBean.setIcon(icon); Log.e(TAG, "上传chenggong "); update(personBean); message.obj = true; bmobHandler.sendMessage(message); } @Override public void onFailure(int i, String s) { Log.e(TAG, "111upLoad:" + i); message.obj = false; bmobHandler.sendMessage(message); } });} else { Toast.makeText(mContext, "文件路径不正确", Toast.LENGTH_SHORT).show();}
} else {Toast.makeText(mContext, "登录超时,请重新登录", Toast.LENGTH_SHORT).show();
}
}
/**
* @param condition 登录账号
* @function 查询用户信息
*/
public void queryPersonMSG(String condition) {
final Message message = new Message();
message.what = BMOB_QUERY_OK;
if (StringUtil.isEmpty(condition)) {Toast.makeText(mContext, "查询条件为空", Toast.LENGTH_SHORT).show();
} else {BmobQuery bq = new BmobQuery<>();bq.addWhereEqualTo("username", condition);bq.findObjects(mContext, new FindListener() { @Override public void onSuccess(List list) { Log.e(TAG, "getUserMSG:" + "获取成功" + list.toString()); message.obj = list; bmobHandler.sendMessage(message); } @Override public void onError(int i, String s) { Log.e(TAG, "getUserMSG:" + "获取失败" + s); message.obj = null; bmobHandler.sendMessage(message); }});
}
}
/**
* @param condition_head 查询头
* @param condition 查询信息
* @function 查询社团信息
*/
public void querySociationMSG(String condition_head, String condition) {
final Message message = new Message();
message.what = BMOB_QUERY_OK;
if (StringUtil.isEmpty(condition) || StringUtil.isEmpty(condition_head)) {Toast.makeText(mContext, "查询条件为空", Toast.LENGTH_SHORT).show();
} else {BmobQuery bq = new BmobQuery<>();bq.addWhereEqualTo(condition_head, condition);bq.findObjects(mContext, new FindListener() { @Override public void onSuccess(List list) { Log.e(TAG, "getUserMSG:" + "获取成功" + list.toString()); message.obj = list; bmobHandler.sendMessage(message); } @Override public void onError(int i, String s) { Log.e(TAG, "getUserMSG:" + "获取失败" + s); message.obj = null; bmobHandler.sendMessage(message); }});
}
}
/**
* @param condition_head 查询头
* @param condition 查询信息
* @function 查询活动信息
*/
public void queryAvtiveMSG(String condition_head, String condition) {
final Message message = new Message();
message.what = BMOB_QUERY_OK;
if (StringUtil.isEmpty(condition) || StringUtil.isEmpty(condition_head)) {Toast.makeText(mContext, "查询条件为空", Toast.LENGTH_SHORT).show();
} else {BmobQuery bq = new BmobQuery<>();bq.addWhereEqualTo(condition_head, condition);bq.findObjects(mContext, new FindListener() { @Override public void onSuccess(List list) { Log.e(TAG, "getUserMSG:" + "获取成功" + list.toString()); message.obj = list; bmobHandler.sendMessage(message); } @Override public void onError(int i, String s) { Log.e(TAG, "getUserMSG:" + "获取失败" + s); message.obj = null; bmobHandler.sendMessage(message); }});
}
}
/**
* @param t
* @param
* @function 修改
*/
public void update(T t) {
final Message message = new Message();
message.what = BMOB_EDIT_OK;
t.update(mContext, new UpdateListener() [email protected] void onSuccess() { Log.e(TAG, "修改成功"); message.obj = true; bmobHandler.sendMessage(message);[email protected] void onFailure(int i, String s) { Log.e(TAG, "修改失败:" + s); message.obj = false; bmobHandler.sendMessage(message);}
});
}
/**范类转换*/
public static Class getGenericSuperclass(Class entity) {
ParameterizedType type = (ParameterizedType) entity.getGenericSuperclass();
Class entityClass = (Class) type.getActualTypeArguments()[0];
return entityClass;
}
}

因为和即时通信用的是环信,所以在登录和注册那儿加了环信的方法。

由于不晓得该怎么得到泛型对象所在的类,方法内的 JAVABEAN参数传不进去,所以查询部分只能用笨方法,就是一个一个查。

本文数据库(综合)相关术语:系统安全软件

主题: JavaGIS数据SGEMCRY数据库EDI
分页:12
转载请注明
本文标题:用到BMOB数据库和登录 自己封装的几个方法
本站链接:http://www.codesec.net/view/480245.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(33)