我们在开发安卓App时难免要与服务器打交道,尤其是对于用户账号信息的注册与登录更是每个Android开发人员必须掌握的技能,本文将对客户端的注册/登录功能的实现进行分析,不到之
我们在开发安卓App时难免要与服务器打交道,尤其是对于用户账号信息的注册与登录更是每个Android开发人员必须掌握的技能,本文将对客户端的注册/登录功能的实现进行分析,不到之处还请指出。
在这里我们仅讨论客户端如何请求服务器进行注册,而服务器在收到客户端请求后进行的一系列操作并不在本文所述范围内,有兴趣大家可以参考
请求服务器
客户端在进行用户信息的注册和登录时一般使用post请求(携带参数)去服务器。以volley框架请求服务器为例,真正与服务器进行交互的就是如下代码:
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);
当然,我们在请求服务器成功或失败时应该设置相应的回调方法,让我们能够进行一些操作。
•callback.onSuccess() //请求成功的回调
1.保存用户注册信息(SP中和Application中)
2.跳转到主页面
•callback.onFailure() //请求失败的回调
1.提示错误信息
下面通过一个具体的demo来介绍
(声明:此demo为阿福老师IT蓝豹App的代码截取)
注:我们在与服务器进行交互时,必须按照规定的接口和规则进行请求,这里使用的是IT蓝豹App中的服务器,服务器的注册接口数据格式如下
1.url:Http://www.itlanbao.com/api/app/users/user_reGISter_Handler.ashx
2.参数说明
nickname 必须有 昵称
email 必须有 邮箱
passWord 必须有 密码
accesstoken 必须有 签名md5(nickname+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":"接口调用失败"
}
demo演示
主要实现代码(demo会在文章最后给出)
1.注册页面中(RegisterActivity),点击注册按钮
registBtn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//获得用户输入的信息
String nick = loginNick.getText().toString();
String emailStr = email.getText().toString();
String passwordStr = password.getText().toString();
if (!TextUtils.isEmpty(nick) &&
!TextUtils.isEmpty(emailStr)
&& !TextUtils.isEmpty(passwordStr)) {
if (Utils.isEmail(emailStr)) {//验证邮箱格式是否符合
//调用RequestApiData中的getRegistData()方法进行注册,传入用户输入的昵称,邮箱、密码,以及解析数据的bean对象和callback对象(回调到自身)
RequestApiData.getInstance().getRegistData(nick, emailStr, passwordStr,
AnalyticalRegistInfo.class, RegisterActivity.this);
} else {
Toast.makeText(RegisterActivity.this, "输入邮箱有误", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(RegisterActivity.this, "输入信息未完全", Toast.LENGTH_SHORT).show();
}
}
});
注意:这个注册的方法中,我们传入的最后一个参数是回调的对象,这里我们传入的是RegisterActivity自身,所以它需要实现
HttpResponeCallBack接口
RequestApiData.getInstance().getRegistData(nick, emailStr, passwordStr,
AnalyticalRegistInfo.class, RegisterActivity.this);
2.请求服务器的回调接口(HttpResponeCallBack)
public interface HttpResponeCallBack {
public void onResponeStart(String apiName);
public void onLoading(String apiName, long count, long current);
public void onSuccess(String apiName, Object object);
public void onFailure(String apiName, Throwable t, int errorNo, String strMsg);
}
3.网络接口类(RequestApiData)
public class RequestApiData {
private static RequestApiData instance = null;
private HttpResponeCallBack mCallBack = null;
//创建接口对象
public static RequestApiData getInstance() {
if (instance == null) {
instance = new RequestApiData();
}
return instance;
}
public void getRegistData(String nickname,String email
,String password, Class<AnalyticalRegistInfo> clazz,
HttpResponeCallBack callback) {
mCallBack = callback;
//这是每一个接口的唯一标示
String tagUrl = UrlConstance.KEY_REGIST_INFO;//注册接口
//将注册的信息保存在map中(须和服务器端一致)
HashMap<String, String> parameter = new HashMap<String, String>();
parameter.put("nickname", nickname);
parameter.put("email",email);
parameter.put("password",password);
//拼接参数信息,昵称,邮箱,密码,公钥,并用md5进行加密
StringBuilder builder = new StringBuilder();
builder.append(nickname);
builder.append(email);
builder.append(password);
builder.append(UrlConstance.PUBLIC_KEY);
parameter.put(UrlConstance.ACCESSTOKEN_KEY,MD5Util.getMD5Str(builder.toString()));
//调用RequestManager的post方法,请求服务器
RequestManager.post(UrlConstance.APP_URL,tagUrl, parameter, clazz, callback);
}
}
4.网络请求处理的类(RequestManager)
public class RequestManager {
private static RequestQueue mRequestQueue;
private static ImageLoader mImageLoader;
private synchronized static void initRequestQueue() {
if (mRequestQueue == null) {
//创建一个请求队列(使用Volley框架)
mRequestQueue = Volley.newRequestQueue(ItLanbaoLibApplication.getInstance());
}
}
private static void addRequest(Request<?> request, Object tag) {
if (tag != null) {
request.setTag(tag);
}
mRequestQueue.add(request);
}
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_register_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.在请求服务器成功/失败后会执行回调方法,而我们传入的callback对象是自身(RegisterActivity),所以现在我们回到注册页面
@Override
public void onResponeStart(String apiName) {
// TODO Auto-generated method stub
Toast.makeText(RegisterActivity.this, "正在请求数据...", Toast.LENGTH_SHORT).show();
}
@Override
public void onLoading(String apiName, long count, long current) {
Toast.makeText(RegisterActivity.this, "Loading...", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(String apiName, Object object) {
// TODO Auto-generated method stub
//注册接口
if (UrlConstance.KEY_REGIST_INFO.equals(apiName)) {
if (object != null && object instanceof AnalyticalRegistInfo) {
AnalyticalRegistInfo info = (AnalyticalRegistInfo) object;
String successCode = info.getRet();
//请求成功
if (successCode.equals(Constant.KEY_SUCCESS)) {
UserBaseInfo baseUser = new UserBaseInfo();
baseUser.setEmail(info.getEmail());
baseUser.setNickname(info.getNickname());
baseUser.setUserhead(info.getUserhead());
baseUser.setUserid(String.valueOf(info.getUserid()));
ItLanBaoApplication.getInstance().setBaseUser(baseUser);
UserPreference.save(KeyConstance.IS_USER_ID, String.valueOf(info.getUserid()));
UserPreference.save(KeyConstance.IS_USER_ACCOUNT, info.getEmail());
UserPreference.save(KeyConstance.IS_USER_PASSWORD, password.getText().toString());
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
RegisterActivity.this.startActivity(intent);
Toast.makeText(RegisterActivity.this, "注册成功...", Toast.LENGTH_SHORT).show();
RegisterActivity.this.finish();
} else {
Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onFailure(String apiName, Throwable t, int errorNo, String strMsg) {
Toast.makeText(RegisterActivity.this, "Failure", Toast.LENGTH_SHORT).show();
}
demo下载地址:http://xiazai.jb51.net/201611/yuanma/Androidlogindemo(jb51.net).rar
至此,安卓客户端的注册功能就实现了,下一篇中将会介绍登录和自动登录的实现,尽请关注。
--结束END--
本文标题: Android客户端实现注册、登录详解(1)
本文链接: https://lsjlt.com/news/23822.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