BaseActivity Demo:
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements IBaseView { public P presenter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(setChildContentView()); initView(); initBaseData(); initData(); } private void initBaseData() { presenter = setPresenter(); if (presenter != null) { presenter.attacheView(this); } else { Log.e("----", "initBaseData: nu" ); try { throw new Exception("你他妈的"); } catch (Exception e) { e.printStackTrace(); } } } ; abstract void initView(); abstract void initData(); abstract P setPresenter(); abstract int setChildContentView(); }
MainActivity Demo:
public class MainActivity extends BaseActivity<MainPresenter> implements IMainView, View.OnClickListener { private TextView zhuce; private EditText mobileLogin; private EditText passwordLogin; private Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override void initView() { mobileLogin = (EditText) findViewById(R.id.mobile_login); passwordLogin = (EditText) findViewById(R.id.password_login); findViewById(R.id.login).setOnClickListener(this); zhuce = (TextView) findViewById(R.id.zhuce); zhuce.setOnClickListener(this); } @Override void initData() { // MainPresenter mainPresenter = new MainPresenter(); // mainPresenter.attacheView(this); if (presenter != null) { presenter.loadDataFrom(); } } @Override MainPresenter setPresenter() { return new MainPresenter(); } @Override int setChildContentView() { return R.layout.activity_main; } @Override public void onSuccess(String str) { Log.e("myMessage", "onSuccess" + str); } @Override protected void onDestroy() { super.onDestroy(); presenter.detachView(); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.zhuce: Intent intent = new Intent(MainActivity.this, RegisterActivity.class); startActivity(intent); finish(); break; case R.id.login: String mobileL = mobileLogin.getText().toString(); String passwordL = passwordLogin.getText().toString(); if (!CommonUtil.isMobileNO(mobileL)) { Toast.makeText(MainActivity.this, getResources().getString(R.string.wrong_mobile_num), Toast.LENGTH_SHORT).show(); return; } if (passwordL == null || "".equals(passwordL)) { Toast.makeText(MainActivity.this, getResources().getString(R.string.wrong_password), Toast.LENGTH_SHORT).show(); return; } String path="user/login"; HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("mobile",mobileL); hashMap.put("password",passwordL); presenter.Login(path,hashMap); break; } } }
BasePresenter Demo:
public class BasePresenter<V extends IBaseView> { private V iBaseView; public void attacheView(V iBaseView) { this.iBaseView = iBaseView; } public void detachView() { iBaseView = null; } public V getView() { return iBaseView; } }
MainPresenter Demo:
public class MainPresenter extends BasePresenter<IMainView>{ private final HttpUtils instance; public MainPresenter() { instance = HttpUtils.getInstance(); } public void loadDataFrom(){ // String data = instance.doPost(); getView().onSuccess("是啥"); } public void Login(String path, HashMap<String ,String> hashMap){ instance.doPost(path, hashMap, new HttpUtilsCallBack() { @Override public void onSuccess(String str) { Log.e("myMessage", str); Looper.prepare(); IMainView view = getView(); Gson gson = new Gson(); LoginBean loginBean = gson.fromJson(str, LoginBean.class); String code = loginBean.getCode(); String msg = loginBean.getMsg(); if (code.equals("1")) { Toast.makeText((Context) view, msg, Toast.LENGTH_SHORT).show(); } else { Toast.makeText((Context) view, msg, Toast.LENGTH_SHORT).show(); } Looper.loop(); } @Override public void onFail(int errCode, String errMsg) { } }); } }
model层接口:
请求数据的工具类:
public class HttpUtils implements Callback{ private String url = "https://www.zhaoapi.cn/user/login?mobile=18238356789&password=123456"; private String string; private static HttpUtils INSTANCE; private final OkHttpClient okHttpClient; private HttpUtilsCallBack mHttpUtilsCallBack; private HttpUtils(){ okHttpClient = new OkHttpClient(); } public static HttpUtils getInstance(){ if (INSTANCE==null){ INSTANCE = new HttpUtils(); } return INSTANCE; } public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); // public String doGet() { // Request request = new Request.Builder() // .url(url) // .build(); // Call call = okHttpClient.newCall(request); // call.enqueue(this); // return "string"; // } public String doPost(String path, HashMap<String,String > map, HttpUtilsCallBack httpUtilsCallBack) { this.mHttpUtilsCallBack=httpUtilsCallBack; FormBody.Builder builder = new FormBody.Builder(); Iterator<String> iterator = map.keySet().iterator(); while (iterator.hasNext()) { String key = iterator.next(); String value = map.get(key); builder.add(key,value); } FormBody body = builder.build(); Request request = new Request.Builder() .url(Constant.BASE_URL+path) .post(body) .build(); Call call = okHttpClient.newCall(request); call.enqueue(this); return "string"; } @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); mHttpUtilsCallBack.onSuccess(string); } }