上文中介绍了安卓客户端与服务器交互,实现注册功能,Android客户端实现注册/登录详解(一) 本文将继续介绍App与服务器的交互实现登录和自动登录的功能,上文说到请求服务器进
上文中介绍了安卓客户端与服务器交互,实现注册功能,Android客户端实现注册/登录详解(一)
本文将继续介绍App与服务器的交互实现登录和自动登录的功能,上文说到请求服务器进行注册主要是通过POST请求携带参数实现,起作用的主要代码:
StringRequest request=new StringRequest(Method.POST, url, new Listener<String>() {
//请求成功
@Override
public void onResponse(String s) {
//执行请求成功的回调
callback.onSuccess()
}
}, new ErrorListener() {
//请求错误
@Override
public void onErrorResponse(VolleyError volleyError) {
//执行请求失败的回调
callback.onFailure()
}
}){
//携带参数(Map集合)
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return parames;
}
};
//将请求添加到请求队列中
Volley.newRequestQueue(context).add(request);
其实登录实现的原理也是一样的,同样是通过POST请求,而在本demo中则是把请求服务器的方法封装在一起了,所以登录的实现也是调用了RequestManager网络请求处理类中的post方法
public static <T> void post(final String app_url, final String tag_url, final HashMap<String, String> parameter, Class<T> clazz,
final HttpResponeCallBack callback) {
//发送post请求服务器
post(app_url, tag_url, parameter, clazz, callback, Priority.NORMAL);
}
demo演示
实现代码
1.服务器的数据格式
1.url: http://www.itlanbao.com/api/app/users/user_login_handler.ashx
2.参数说明:
email 必须有 邮箱
passWord 必须有 密码
accesstoken 必须有 md5(email+password+"双方平台约定公钥")
3.请求方式:POST
4.返回值格式
成功
{
"ret":0,
"errcode":0,
"msg":"登录用户接口调用成功",
"data":{
"userid":"16489",
"email":"nnn@aaa.com",
"nickname":"duss",
"userhead":"http://img.itlanbao.com/avatar.png"
}
}
失败
{
"ret":1,
"errcode":1,
"msg":"账号或密码错误"
}
2.登录界面(LoginActivity),点击登录按钮
//点击登录按钮
loginBtn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String account = loginAccount.getText().toString();//账号
String password = loginPassword.getText().toString();//密码
if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password)
&& Utils.isEmail(account)) {
RequestApiData.getInstance().getLoginData(account, password, UserBaseInfo.class, LoginActivity.this);
} else {
Toast.makeText(LoginActivity.this, "账号或者密码有误", Toast.LENGTH_SHORT).show();
}
}
});
核心代码为:
//传入账号名,密码,解析数据的bean对象和回调(这里传入的是自身,所以LoginActivity也同样实现了回调接口HttpResponeCallBack)
RequestApiData.getInstance().getLoginData(account, password, UserBaseInfo.class, LoginActivity.this);
3.网络接口类(RequestApiData)
//创建接口对象
public static RequestApiData getInstance() {
if (instance == null) {
instance = new RequestApiData();
}
return instance;
}
public void getLoginData(String email ,String password,
Class<UserBaseInfo> clazz,
HttpResponeCallBack callback) {
mCallBack = callback;
//这是每一个接口的唯一标示
String tagUrl = UrlConstance.KEY_LOGIN_INFO;//登录接口
HashMap<String, String> parameter = new HashMap<String, String>();
parameter.put("email", email);
parameter.put("password", password);
//拼接参数信息,邮箱,密码,公钥,并用md5进行加密
StringBuilder builder = new StringBuilder();
builder.append(email);
builder.append(password);
builder.append(UrlConstance.PUBLIC_KEY);
parameter.put(UrlConstance.ACCESSTOKEN_KEY,MD5Util.getMD5Str(builder.toString()));
//请求数据接口
RequestManager.post(UrlConstance.APP_URL,tagUrl, parameter, clazz, callback);
}
4.网络请求处理类(RequestManager)中请求数据,和注册执行了同样的方法,只是这里的传入的tag_url为登录的接口
public static <T> void post(final String app_url, final String tag_url, final HashMap<String, String> parameter, Class<T> clazz,
final HttpResponeCallBack callback) {
//发送post请求服务器
post(app_url, tag_url, parameter, clazz, callback, Priority.NORMAL);
}
public static <T> void post(final String app_url, final String url, final HashMap<String, String> parameter, final Class<T> clazz,
final HttpResponeCallBack callback, Priority priority) {
if (callback != null) {
callback.onResponeStart(url);//回调请求开始
}
initRequestQueue();
//将公共的接口前缀和接口名称拼接
//eg:拼接成登录的接口 http://www.itlanbao.com/api/app/users/user_login_handler.ashx
StringBuilder builder = new StringBuilder(app_url);
builder.append(url);
{// 检查当前网络是否可用
final NetworkUtils networkUtils = new NetworkUtils(ItLanbaoLibApplication.getInstance());
if (!networkUtils.isNetworkConnected() && android.os.Build.VERSION.SDK_INT > 10) {
if (callback != null) {
callback.onFailure(url, null, 0, "网络出错");//回调请求失败
return;
}
}
}
StringRequest request = new StringRequest(Method.POST, builder.toString(),
new Listener<String>() {
@Override
public void onResponse(String response) {
// TODO Auto-generated method stub
// 这个位置先公共解析处理共同异常
try {
if (response != null && callback != null) {
Gson gson = new Gson();
//回调请求成功,同时url和解析的对象
callback.onSuccess(url, gson.fromJSON(response, clazz));
}
} catch (Exception e) {
// TODO: handle exception
if (callback != null) {
//回调请求失败--解析异常
callback.onFailure(url, e, 0, "解析异常");
return;
}
}
}
}, new ErrorListener() {
//请求出错的监听
@Override
public void onErrorResponse(VolleyError error) {
if (callback != null) {
if (error != null) {
callback.onFailure(url, error.getCause(), 0,
error.getMessage());
} else {
callback.onFailure(url, null, 0, "");
}
}
}
}) {
//post请求的参数信息
protected Map<String, String> getParams() {
return getPostApiParmes(parameter);
}
};
//添加请求到请求队列中
addRequest(request, url);
}
private static ApiParams getPostApiParmes(final HashMap<String, String> parameter) {
ApiParams api = new ApiParams();
for (Entry<String, String> entry : parameter.entrySet()) {
api.with(entry.geTKEy(), entry.getValue());
}
return api;
}
5.同样回到LoginActivity中执行回调,失败则提示,成功则将登录信息保存到SP中和Application中
@Override
public void onResponeStart(String apiName) {
// TODO Auto-generated method stub
if (UrlConstance.KEY_LOGIN_INFO.equals(apiName)) {
Toast.makeText(LoginActivity.this, "正在加载数据中", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLoading(String apiName, long count, long current) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(String apiName, Object object) {
// TODO Auto-generated method stub
if (UrlConstance.KEY_LOGIN_INFO.equals(apiName)) {
//邮箱登录返回数据
if (object != null && object instanceof UserBaseInfo) {
UserBaseInfo info = (UserBaseInfo) object;
if (info.getRet().equals(Constant.KEY_SUCCESS)) {
//登录成功,保存登录信息
ItLanBaoApplication.getInstance().setBaseUser(info);//保存到Application中
//保存到SP中
UserPreference.save(KeyConstance.IS_USER_ID, String.valueOf(info.getUserid()));
UserPreference.save(KeyConstance.IS_USER_ACCOUNT, info.getEmail());
UserPreference.save(KeyConstance.IS_USER_PASSWORD, loginPassword.getText().toString());
Intent intent = new Intent();
intent.setClass(LoginActivity.this, MainActivity.class);
startActivity(intent);
overridePendingTransition(android.R.anim.slide_in_left,
android.R.anim.slide_out_right);
finish();
} else {
Log.e("TAG", "info="+info.toString());
if (info.getErrcode().equals(Constant.KEY_NO_REGIST)) {
Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, info.getMsg(), Toast.LENGTH_SHORT).show();
Log.e("TAG", "info.getMsg()="+info.getMsg());
}
}
}
}
}
@Override
public void onFailure(String apiName, Throwable t, int errorNo,
String strMsg) {
// TODO Auto-generated method stub
Toast.makeText(LoginActivity.this, "Failure", Toast.LENGTH_SHORT).show();
}
6.自动登陆的实现,其实就是我们在欢迎页面进行一个判断:读取SP中的信息,如有登录的信息,则取出,携带此信息请求服务器(同登录的请求),若成功,则直接跳转到主页面;如果登录不成功或者SP中没有保存的登录信息,则跳转到登录界面,代码如下(WelcomeActivity中)
String userAccount = UserPreference.read(KeyConstance.IS_USER_ACCOUNT, null);//软件还没有保持账号
String userPassword = UserPreference.read(KeyConstance.IS_USER_PASSWORD, null);
String userid = UserPreference.read(KeyConstance.IS_USER_ID, null);
if (TextUtils.isEmpty(userAccount)) {//没有保存的登录信息跳转到登录界面
//空的,表示没有注册,或者清除数据
Intent intent = new Intent();
intent.setClass(WelcomeActiviy.this, LoginActivity.class);
startActivity(intent);
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
finish();
} else {
//用保存的信息直接登录
RequestApiData.getInstance().getLoginData(userAccount, userPassword,
UserBaseInfo.class, WelcomeActiviy.this);
}
WelcomeActivity也同样实现了HttpResponeCallBack接口,所以传入的callback对象也是自身,我们在回调方法中判断是否登录成功
@Override
public void onResponeStart(String apiName) {
}
@Override
public void onLoading(String apiName, long count, long current) {
}
@Override
public void onSuccess(String apiName, Object object) {
//当前接口是否是获取用户的基本信息的接口
if (UrlConstance.KEY_USER_BASE_INFO.equals(apiName)) {
if (object != null && object instanceof UserBaseInfo) {
UserBaseInfo info = (UserBaseInfo) object;
ItLanBaoApplication.getInstance().setBaseUser(info);//把数据放入到Application里面,全局
UserPreference.save(KeyConstance.IS_USER_ID, String.valueOf(info.getUserid()));
Intent intent = new Intent();
intent.setClass(WelcomeActiviy.this, MainActivity.class);
startActivity(intent);
overridePendingTransition(android.R.anim.slide_in_left,
android.R.anim.slide_out_right);
finish();
} else {
Toast.makeText(WelcomeActiviy.this, "加载失败", Toast.LENGTH_SHORT).show();
}
} else if (UrlConstance.KEY_LOGIN_INFO.equals(apiName)) {//当前接口是登录的接口
//登录返回数据
if (object != null && object instanceof UserBaseInfo) {
UserBaseInfo info = (UserBaseInfo) object;
if (Constant.KEY_SUCCESS.equals(info.getRet())) {
ItLanBaoApplication.getInstance().setBaseUser(info);//将用户信息保存在Application中
UserPreference.save(KeyConstance.IS_USER_ID, String.valueOf(info.getUserid()));
Intent intent = new Intent();
intent.setClass(WelcomeActiviy.this, MainActivity.class);
startActivity(intent);
overridePendingTransition(android.R.anim.slide_in_left,
android.R.anim.slide_out_right);
finish();
} else {
Toast.makeText(WelcomeActiviy.this, info.getMsg(), Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onFailure(String apiName, Throwable t, int errorNo, String strMsg) {
Toast.makeText(WelcomeActiviy.this, "Failure", Toast.LENGTH_SHORT).show();
}
demo下载地址:http://xiazai.jb51.net/201611/yuanma/Androidlogindemo(jb51.net).rar
您可能感兴趣的文章:Android登录注册功能 数据库sqlite验证Android手机注册登录时获取验证码之后倒计时功能(知识点总结)Android实现登录注册功能封装Android实现闪屏及注册和登录界面之间的切换效果Android客户端实现注册、登录详解(1)Android注册登录实时自动获取短信验证码Android设计登录界面、找回密码、注册功能Android开发之注册登录方法示例Android封装MVP实现登录注册功能
--结束END--
本文标题: Android客户端实现注册、登录详解(2)
本文链接: https://lsjlt.com/news/23063.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0