1.IView View层
package com.example.lenovo.logindemo02.view; import android.content.Context; /** * */ public interface IView{ void success(T t); void failed(Exception e); String getMobile(); //p层验证用 String getPassword(); void setMobile(String mobile); void setPassword(String password); void check(boolean isChecked); void show(); void dissmiss(); void gotoMain(); Context getContext(); }
/ /MainActivity
package com.example.lenovo.logindemo02.view; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.example.lenovo.logindemo02.R; import com.example.lenovo.logindemo02.bean.LoginBean; import com.example.lenovo.logindemo02.presenter.LoginPresenter; public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener { private EditText edtMobile; private EditText edtPassword; private Button btnLogin; private TextView txtZhuce; private LoginPresenter presenter; private ProgressDialog pd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { presenter = new LoginPresenter(); presenter.attach(this); presenter.isFirst(); pd = new ProgressDialog(this); pd.setMessage("正在登录,请稍后..."); } private void initView() { edtMobile = findViewById(R.id.edt_mobile); edtPassword = findViewById(R.id.edt_password); btnLogin = findViewById(R.id.btn_login); txtZhuce = findViewById(R.id.txt_zhuce); btnLogin.setOnClickListener(this); } @Override public void success(LoginBean loginBean) { if (loginBean != null){ Toast.makeText(this, loginBean.getMsg(), Toast.LENGTH_SHORT).show(); } } @Override public void failed(Exception e) { Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show(); } @Override public String getMobile() { return edtMobile.getText().toString().trim(); } @Override public String getPassword() { return edtPassword.getText().toString(); } @Override public void setMobile(String mobile) { edtMobile.setText(mobile); } @Override public void setPassword(String password) { edtPassword.setText(password); } @Override public void check(boolean isChecked) { if (isChecked){ presenter.login("https://www.zhaoapi.cn/user/login"); } } @Override public void show() { pd.show(); } @Override public void dissmiss() { pd.dismiss(); } @Override public void gotoMain() { Intent intent = new Intent(MainActivity.this,HomeActivity.class); startActivity(intent); finish(); } @Override public Context getContext() { return this; } //点击 @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_login: presenter.check(); break; } } @Override protected void onDestroy() { super.onDestroy(); if(presenter != null){ presenter.detach(); } } }
2. Model层
public class LoginModel { public void login(String url, ICallBack callBack , Type type){ HttpUtils.getInstance().get(url,callBack,type); } }
2.1 HttpUtils
package com.example.lenovo.logindemo02.utils; import android.os.Handler; import com.example.lenovo.logindemo02.inter.ICallBack; import com.google.gson.Gson; import java.io.IOException; import java.lang.reflect.Type; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; /** * */ public class HttpUtils { private static volatile HttpUtils instance; private OkHttpClient client; private Handler handler = new Handler(); private HttpUtils(){ client = new OkHttpClient(); } public static HttpUtils getInstance(){ if (instance == null){ synchronized (HttpUtils.class){ if (null == instance){ instance = new HttpUtils(); } } } return instance; } public void get(String url, final ICallBack callBack, final Type type){ Request request = new Request.Builder() .get() .url(url) .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { callBack.onFailed(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { String result = response.body().string(); Gson gson = new Gson(); final Object o = gson.fromJson(result, type); handler.post(new Runnable() { @Override public void run() { callBack.onSuccess(o); } }); } }); } }
2.2 ICallBack
package com.example.lenovo.logindemo02.inter; /** * */ public interface ICallBack { void onSuccess(Object obj); void onFailed(Exception e); }
3. Presenter层
package com.example.lenovo.logindemo02.presenter; import android.content.Context; import android.content.SharedPreferences; import android.text.TextUtils; import com.example.lenovo.logindemo02.bean.LoginBean; import com.example.lenovo.logindemo02.inter.ICallBack; import com.example.lenovo.logindemo02.model.LoginModel; import com.example.lenovo.logindemo02.view.IView; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; /** * */ public class LoginPresenter { private IView iv; private LoginModel loginModel; public void attach(IView iv){ this.iv = iv; loginModel = new LoginModel(); } public void detach(){ if (iv != iv){ iv=iv; } } //检验 public void check(){ if (TextUtils.isEmpty(iv.getMobile()) || TextUtils.isEmpty(iv.getPassword())){ iv.check(false); }else { iv.check(true); } } public void isFirst(){ SharedPreferences sp = iv.getContext().getSharedPreferences("config", Context.MODE_PRIVATE); String mobile = sp.getString("mobile", ""); String password = sp.getString("password",""); //不是第一次登录 if (! TextUtils.isEmpty(mobile) &&!TextUtils.isEmpty(password)){ iv.setMobile(mobile); iv.setPassword(password); } } public void login(String url){ iv.show(); final String mobile = iv.getMobile(); final String password = iv.getPassword(); url = url.concat("?mobile=").concat(mobile).concat("&password=").concat(password); Type type = new TypeToken(){}.getType(); loginModel.login(url, new ICallBack() { @Override public void onSuccess(Object obj) { iv.dissmiss(); iv.success(obj); LoginBean loginBean = (LoginBean) obj; if (loginBean.getCode().equals("0")){ iv.gotoMain(); } SharedPreferences sp = iv.getContext().getSharedPreferences("config",Context.MODE_PRIVATE); sp.edit().putString("mobile",mobile) .putString("password",password) .commit(); } @Override public void onFailed(Exception e) { iv.dissmiss(); iv.failed(e); } },type); } }