1.登录功能
用户登陆使用手机号+短信验证码的方式登陆,服务器端并不保存用户手机号。
登陆成功后客户端可以获取到登陆标识,客户端可以通过登陆标识保持相对长时间的访问服务器的权限。
2.接口
获得验证码:
class="brush:java;gutter:true;">{\"status\":1}
匿名Login:
{\"status\":1,\"token\":\"asdasdasdsadasd\"}
3.代码
MainActivity
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String token = Config.getCachedToken(this); String phone_num = Config.getCachedPhoneNum(this); if (token!=null&&phone_num!=null) { Intent i =new Intent(this, AtyTimeline.class); i.putExtra(Config.KEY_TOKEN, token); i.putExtra(Config.KEY_PHONE_NUM, phone_num); startActivity(i); }else{ startActivity(new Intent(this, AtyLogin.class)); } finish(); }
LoginActivity
findViewById(R.id.btnGetCode).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if (TextUtils.isEmpty(etPhone.getText())) { Toast.makeText(AtyLogin.this, R.string.phone_num_can_not_be_empty, Toast.LENGTH_LONG).show(); return; } final ProgressDialog pd = ProgressDialog.show(AtyLogin.this, getResources().getString(R.string.connecting), getResources().getString(R.string.connecting_to_server)); new GetCode(etPhone.getText().toString(), new GetCode.SuccessCallback() { @Override public void onSuccess() { pd.dismiss(); Toast.makeText(AtyLogin.this, R.string.suc_to_get_code, Toast.LENGTH_LONG).show(); } }, new GetCode.FailCallback() { @Override public void onFail() { pd.dismiss(); Toast.makeText(AtyLogin.this, R.string.fail_to_get_code, Toast.LENGTH_LONG).show(); } }); } }); findViewById(R.id.btnLogin).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if (TextUtils.isEmpty(etPhone.getText())) { Toast.makeText(AtyLogin.this, R.string.phone_num_can_not_be_empty, Toast.LENGTH_LONG).show(); return; } if (TextUtils.isEmpty(etCode.getText())) { Toast.makeText(AtyLogin.this, R.string.code_can_not_be_empty, Toast.LENGTH_LONG).show(); return; } final ProgressDialog pd = ProgressDialog.show(AtyLogin.this, getResources().getString(R.string.connecting), getResources().getString(R.string.connecting_to_server)); new Login(MD5Tool.md5(etPhone.getText().toString()), etCode.getText().toString(), new Login.SuccessCallback() { @Override public void onSuccess(String token) { pd.dismiss(); Config.cacheToken(AtyLogin.this, token); Config.cachePhoneNum(AtyLogin.this, etPhone.getText().toString()); Intent i = new Intent(AtyLogin.this, AtyTimeline.class); i.putExtra(Config.KEY_TOKEN, token); i.putExtra(Config.KEY_PHONE_NUM, etPhone.getText().toString()); startActivity(i); finish(); } }, new Login.FailCallback() { @Override public void onFail() { pd.dismiss(); Toast.makeText(AtyLogin.this, R.string.fail_to_login, Toast.LENGTH_LONG).show(); } }); } });