接上个问题,贴出代码
public class LoginActivity extends BaseActivity{ private static final String KEY_USERNAME = "key_username"; private static final String KEY_USERPASSWORD = "key_userpassword"; private EditText etUser; private EditText etPassword; private Button login; private TextView register; //这是一个逻辑类 private UserBiz mUserBiz = new UserBiz(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initView(); initIntent(); } private void initIntent() { Intent intent = getIntent(); if (intent != null) { String username = intent.getStringExtra(KEY_USERNAME); Log.e("TAGG","测试 :" + username); if (username != null) { Log.e("TAGG","测试 :" + username); etUser.setText(username); } String password = intent.getStringExtra(KEY_USERPASSWORD); if (password != null) { Log.e("TAGG","测试 :" + password); etPassword.setText(password); } } } private void initView() { etUser = findViewById(R.id.id_et_username); etPassword = findViewById(R.id.id_et_password); login = findViewById(R.id.id_btn_login); register = findViewById(R.id.id_btn_register); } public void click(View view) { switch (view.getId()) { case R.id.id_btn_login: login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String userName = etUser.getText().toString(); String passWord = etPassword.getText().toString(); //判断内容是否为空 if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(passWord)) { Toast.makeText(LoginActivity.this, "账号或者密码不能为空", Toast.LENGTH_SHORT).show(); return; } // 加载进度框 startLoadingProgress(); mUserBiz.login(userName, passWord, new CommonCallback<User>() { @Override public void onError(Exception e) { //加载完成,取消加载进度框 stopLoadingProgress(); Log.e("TAG","测试onError"); Toast.makeText(LoginActivity.this, "账号或者密码错误", Toast.LENGTH_SHORT).show(); } @Override public void onSuccess(User user) { stopLoadingProgress(); //对账号的一个保存 UserInfoHolder.getInstance().setUser(user); toMainActivity(); finish(); } }); } }); break; case R.id.id_btn_register: register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { toRegisterActivity(); } }); break; } } private void toRegisterActivity() { Intent intent = new Intent(LoginActivity.this, RegisterActivity.class); startActivity(intent); } private void toMainActivity() { Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); } public static void launch(Context context, String userName, String password) { Intent intent = new Intent(context,LoginActivity.class); intent.putExtra(KEY_USERNAME,userName); intent.putExtra(KEY_USERPASSWORD,password); context.startActivity(intent); } //我也不知道里面代码到底干嘛的 @Override protected void onResume() { super.onResume(); CookieJarImpl cookieJar = (CookieJarImpl) OkHttpUtils.getInstance().getOkHttpClient().cookieJar(); cookieJar.getCookieStore().removeAll(); } }
上面是登录页的 下面是 注册页的
public class RegisterActivity extends BaseActivity { private EditText mEtUsername; private EditText mEtPassword; private EditText mEtRePassword; private Button mBtnRegister; private UserBiz mUserBiz; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); setUpToolbar(); initView(); initEvent(); mUserBiz = new UserBiz(); setTitle("注册"); } private void initEvent() { mBtnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String userName = mEtUsername.getText().toString(); String paw = mEtPassword.getText().toString(); String rePaw = mEtRePassword.getText().toString(); if (TextUtils.isEmpty(userName)||TextUtils.isEmpty(paw)){ Toast.makeText(RegisterActivity.this, "账号或者密码不能为空", Toast.LENGTH_SHORT).show(); return; } if (!paw.equals(rePaw)){ Toast.makeText(RegisterActivity.this, "请输入相同密码", Toast.LENGTH_SHORT).show(); return; } //加载框 startLoadingProgress(); mUserBiz.register(userName, paw, new CommonCallback<User>() { @Override protected void onError(Exception e) { stopLoadingProgress(); Toast.makeText(RegisterActivity.this, "账号或者密码错误", Toast.LENGTH_SHORT).show(); } @Override protected void onSuccess(User response) { stopLoadingProgress(); Log.e("TAGG","测试Rig :" + response.getUserName()); Log.e("TAGG","测试Rig :" + response.getPassword()); LoginActivity.launch(RegisterActivity.this,response.getUserName(),response.getPassword()); finish(); } }); } }); } private void initView() { mEtUsername = findViewById(R.id.id_et_username); mEtPassword = findViewById(R.id.id_et_password); mEtRePassword = findViewById(R.id.id_et_repassword); mBtnRegister = findViewById(R.id.id_btn_register); } //设置导航栏 protected void setUpToolbar() { Toolbar toolbar = findViewById(R.id.id_toolbar); setSupportActionBar(toolbar); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onBackPressed(); } }); } @Override public void onDetachedFromWindow() { super.onDetachedFromWindow(); mUserBiz.onDestory(); } }
下面就是逻辑类 UserBiz
public class UserBiz { public void onDestory() { OkHttpUtils.getInstance().cancelTag(this); } //这是注册的逻辑实现 public void register(String username, String password, CommonCallback<User> callBack) { OkHttpUtils .post() .url(Config.baseUrl + "user_register") .tag(this) .addParams("username", username) .addParams("password", password) .build() .execute(callBack); } //这是登录的逻辑实现 public void login(String username, String password, CommonCallback<User> callBack) { OkHttpUtils .post() .url(Config.baseUrl + "user_login") .tag(this) .addParams("username", username) .addParams("password", password) .build() .execute(callBack); } }
4
收起
正在回答
1回答
接你http://class.imooc.com/course/qadetail/63982问题。你得从源头找,CommonCallback中调 onSuccess()前:1)看一下是否是onResponse返回的问题,User对象是否正常,名字是否返回的就是一个空。2)看一下GsonUtil转成对象时是否有误,这时getUserName()是否有值。3)User 类中的名字是否有对应set,get方法,方法是否正确。4)User类 中属性名与后台返回的key是否一致。
Android网络操作与数据存储2018版
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星