protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loginl);
ctx=this;
initUI();
initData();
}
private void initData() {
tv_regist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(ctx,regist.class);
startActivity(intent);
}
});
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent intent = new Intent(loginl.this, zhuActivity.class);
// startActivity(intent);
//Intent intent=new Intent(ctx,badidu.class);
//获取用户输入的用户名和密码
phone = et_phone.getText().toString().trim();
pwdStr = et_pwd.getText().toString().trim();
UserBean userBean = new UserBean();
userBean.phone=phone;
userBean.password=pwdStr;
//判断用户输入的用户名和密码是否为空
if(TextUtils.isEmpty(phone)||TextUtils.isEmpty(pwdStr)){
Toast.makeText(ctx,"手机号或密码不能为空",Toast.LENGTH_SHORT).show();
return;
}else{
//判断记住密码是否选中
if(cb.isChecked()){
//选中之后对用户输入的密码进行保存
SpUtils.putStringValue(ctx,"phone",phone);
SpUtils.putStringValue(ctx,"pwd",pwdStr);
}else{
SpUtils.putStringValue(ctx,"phone","");
SpUtils.putStringValue(ctx,"pwd","");
}
//如果用户名和密码都不为空连接服务器进行判断用户名和密码是否正确;
if (checkNetwork()) {
DbUtils.requestHttp(userBean, Constant.LOGIN, ctx);
}else {
Toast.makeText(ctx,"断网了",Toast.LENGTH_LONG).show();
}
}
}
});
}
private void initUI() {
et_phone = (EditText) findViewById(R.id.et_phone);
et_pwd = (EditText) findViewById(R.id.et_pwd);
btn_login = (Button) findViewById(R.id.btn_login);
cb = (CheckBox) findViewById(R.id.cb);
tv_regist = (TextView) findViewById(R.id.tv_regist);
String phone = SpUtils.getStringValue(ctx, "phone", "");
String pwd = SpUtils.getStringValue(ctx, "pwd", "");
if(!TextUtils.isEmpty(phone)&&!TextUtils.isEmpty(pwd)){
et_phone.setText(phone);
et_pwd.setText(pwd);
cb.setChecked(true);
}
}
private boolean checkNetwork() {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connManager.getActiveNetworkInfo() != null) {
return connManager.getActiveNetworkInfo().isAvailable();
}
return false;
}
}
上面是登录.java
tijiao= (Button) findViewById(R.id.tijiao);
tijiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = et_name.getText().toString().trim();
String password = et_password.getText().toString().trim();
String sex = et_sex.getText().toString().trim();
String age = et_age.getText().toString().trim();
String phone = et_phone.getText().toString().trim();
String address = et_address.getText().toString().trim();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password) ||
TextUtils.isEmpty(sex) || TextUtils.isEmpty(age) ||
TextUtils.isEmpty(phone) || TextUtils.isEmpty(address)) {
return;
} else {
UserBean userBean = new UserBean();
userBean.username = name;
userBean.password = password;
userBean.sex = sex;
userBean.age = age;
userBean.phone = phone;
userBean.address = address;
DbUtils.requestHttp(userBean, Constant.REGIST,getApplication());
}
}
});
上面是注册中提交按钮的部分代码
public static void requestHttp(Object object, String url, final Context ctx )
{ RequestQueue queue = NoHttp.newRequestQueue();
// String path = "http://192.168.0.100:8080/Gson/com.servlet/RegistServlet";
Request<String> request = NoHttp.createStringRequest(url, RequestMethod.POST);
Gson gson = new Gson();
String json = gson.toJson(object);
request.add("lrs", json);
queue.add(88, request, new OnResponseListener<String>() {
@Override
public void onStart(int i) {
}
@Override
public void onSucceed(int i, Response<String> response) {
MyUtils.Toast(ctx,"连接成功"+String.valueOf(response.get()));
parseResult((Activity) ctx,response.get());
}
@Override
public void onFailed(int i, String s, Object o, Exception e, int i1, long l) {
MyUtils.Toast(ctx,"连接失败");
}
@Override
public void onFinish(int i) {
}
});
}
private static void parseResult(Activity ctx, String result) {
try {
JSONObject js = new JSONObject(result);
int code = js.getInt("code");
String desc = js.getString("desc");
if (code == 0) {
MyUtils.Toast(ctx, desc);
} else if (code == 1) {//注册
System.out.print("code");
MyUtils.Toast(ctx,"连接成功12346"+js);
Intent intent = new Intent(ctx, loginl.class);
ctx.startActivity(intent);
ctx.finish();
} else if (code == 2) {
//登录成功
Intent intent = new Intent(ctx, zhuActivity.class);
ctx.startActivity(intent);
ctx.finish();
System.out.print("chenggong");
MyUtils.Toast(ctx,"登录成功");
SpUtils.putStringValue(ctx, "userphone", desc);
} else if (code == 3) {
// MyUtils.Toast(ctx, desc);
}else if(code==4){
Intent intent=new Intent(ctx,FindActivity.class);
intent.putExtra("desc",desc);
ctx.startActivity(intent);
}else if(code==5){
Intent intent=new Intent(ctx,AddContactActivity.class);
intent.putExtra("desc",desc);
ctx.startActivity(intent);
}else if(code==6){
MyUtils.Toast(ctx,desc);
}else if(code==7){
MyUtils.Toast(ctx,desc);
}else if(code==8){
MyUtils.Toast(ctx,desc);
}else if(code==9){
MyUtils.Toast(ctx,desc);
//Intent intent=new Intent(ctx, UserInfoActivity.class);
// ctx.startActivity(intent);
// ctx.finish();
}else if(code==10){
MyUtils.Toast(ctx,desc);
}else if(code==11){
// 已添加过紧急联系人
MyUtils.Toast(ctx,desc);
}else if(code==12){
// 添加成功
MyUtils.Toast(ctx,desc);
}else if(code==13){
// 紧急联系人设置已达上限
MyUtils.Toast(ctx,desc);
}else if(code==14){
//得到添加好友
SpUtils.putStringValue(ctx, "contactbean", desc);
}else if(code==15){
//好友资料
Intent intent=new Intent(ctx, ContactInfoActivity.class);
intent.putExtra("desc",desc);
ctx.startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void addressHttp(Object object, String url, final Context ctx ){
Gson gson = new Gson();
String json = gson.toJson(object);
RequestParams request = new RequestParams(url);
request.addParameter("lrs",json);
x.http().post(request, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
上面是DbUtils.java
package com.atguigu.ditu3.activity.bean;
public class UserBean {
public String phone;
public String password;
public String username;
public String sex;
public String age;
public String address;
}
上面是UserBean.java