设计了登录功能,用户在登录时输入账号后会像服务器发送请求,服务器会对请求的id和password和数据库内容进行对比,并把结果返回app,用户会得到提示:此用户不存在,密码错误,
如果正确则会进入用户主界面。
登录功能的实现要配合注册功能,首先是注册的时候将信息保存在了用户表,当用户登录的时候会去从服务器的数据库中读取相关的内容来与用户信息进行匹配,会根据用户输入的账号密码来来判断用户是否存在以及登录时的信息是否正确,然后将结果返回给用户,
代码如下:
public void denglu(View view){
progressDialog = MaskUtil.showProgressDialog("登陆中",MainActivity.this);
EditText EditDLid=(EditText)findViewById(R.id.DLID);
String DLId= EditDLid.getText().toString();
EditText EditDLPA=(EditText)findViewById(R.id.DLPA);
String DLPA= EditDLPA.getText().toString();
String path = "";
OkHttpClient client = new OkHttpClient();
FormBody.Builder requestBuild = new FormBody.Builder();
//添加请求体
RequestBody requestBody = requestBuild
.add("id", DLId)
.add("password", DLPA)
.build();
Request request = new Request.Builder()
.url(path)
.post(requestBody)
.build();
client.newCall(request).enqueue(new Callback() {
//失败
@Override
public void onFailure(Call call, IOException e) {
Looper.prepare();
progressDialog.dismiss();
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setMessage("网络或服务器原因登录失败");
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "我知道了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
Looper.loop();
}
//成功
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
progressDialog.dismiss();
if(result.equals("0")) {
Looper.prepare();
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setMessage("该用户不存在");
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "我知道了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
Looper.loop();
}
else if(result.equals("1")){
Looper.prepare();
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setMessage("密码错误");
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "我知道了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
Looper.loop();
}
else if(result.equals("2")){
//Looper.prepare();
//Intent intent = new Intent(MainActivity.this,User.class);
netweb();
//finish();
//intent.putExtra("ThisID",DLId);
//intent.putExtra("MYTP",list.get(0).getTupian());
//startActivity(intent);
//finish();
//Looper.loop();
}
if (response.body() != null) {
response.body().close();
}
}
});
这是登录部分的代码,用户将信息输入后服务器对数据进行比对并返回结果。