// Modle
public class ModelImpl implements IContract.IModel {
private static final String POST_UTILS = "https://www.zhaoapi.cn/user/reg";
private static final String POST_UTILSS = "https://www.zhaoapi.cn/user/login";
private static final String POST_UTILSSHOW = "https://www.zhaoapi.cn/product/searchProducts";
@Override
public void requestMsg(String username, String password, final onCallBack onCallBack) {
if (username.equals("")||password.equals("")){
onCallBack.responseMsg("用户名或密码不能为空");
return;
}
FormBody formBody = new FormBody.Builder()
.add("mobile",username)
.add("password",password)
.build();
OKHttpUtils.getInstance().post(POST_UTILS, formBody, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String errorMag = e.getMessage().toString();
onCallBack.responseMsg(errorMag);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseMsg = response.body().string();
onCallBack.responseMsg(responseMsg);
}
});
}
@Override
public void requestMsgs(String username, String password, final onCallBack onCallBack) {
FormBody formBody = new FormBody.Builder()
.add("mobile",username)
.add("password",password)
.build();
OKHttpUtils.getInstance().post(POST_UTILSS, formBody, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String errorMsg = e.getMessage().toString();
onCallBack.responseMsg(errorMsg);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseMsg = response.body().string();
onCallBack.responseMsg(responseMsg);
}
});
}
@Override
public void requestMsgshow(String names, final onCallBack onCallBack) {
FormBody formBody = new FormBody.Builder()
.add("keywords",names)
.add("page","1")
.build();
OKHttpUtils.getInstance().post(POST_UTILSSHOW, formBody, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseMsg = response.body().string();
onCallBack.responseMsg(responseMsg);
}
});
}
}
// Presenter
public class PresenterImpl implements IContract.IPresenter<IContract.IView> {
IContract.IView iView;
private IContract.IModel model;
private WeakReference<IContract.IView> iViewWeakReference;
private WeakReference<IContract.IModel> iModelWeakReference;
@Override
public void attachView(IContract.IView iView) {
this.iView = iView;
model = new ModelImpl();
iViewWeakReference = new WeakReference<>(iView);
iModelWeakReference = new WeakReference<>(model);
}
@Override
public void detachView(IContract.IView iView) {
iViewWeakReference.clear();
iModelWeakReference.clear();
}
@Override
public void requestInfo(String username, String password) {
model.requestMsg(username,password,new IContract.IModel.onCallBack() {
@Override
public void responseMsg(String responseMsg) {
iView.showData(responseMsg);
}
});
}
@Override
public void requestInfos(String username, String password) {
model.requestMsgs(username, password, new IContract.IModel.onCallBack() {
@Override
public void responseMsg(String responseMsg) {
iView.showData(responseMsg);
}
});
}
@Override
public void requestInfoes(String names) {
model.requestMsgshow(names, new IContract.IModel.onCallBack() {
@Override
public void responseMsg(String responseMsg) {
iView.showDatas(responseMsg);
}
});
}
}
// View
public class MainActivity extends AppCompatActivity implements IContract.IView {
@BindView(R.id.edit_username)
EditText editUsername;
@BindView(R.id.edit_password)
EditText editPassword;
@BindView(R.id.but_config)
Button butConfig;
private IContract.IPresenter<IContract.IView> presenter;
private String username;
private String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
presenter = new PresenterImpl();
presenter.attachView(this);
}
@Override
public void showData(final String responseMsg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (username.equals("")||password.equals("")){
Toast.makeText(MainActivity.this, "用户或密码不能为空", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, responseMsg, Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,WelloActivity.class));
}
}
});
}
@Override
public void showDatas(final String responseMsg) {
}
@OnClick(R.id.but_config)
public void onViewClicked() {
username = editUsername.getText().toString();
password = editPassword.getText().toString();
presenter.requestInfo(username, password);
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detachView(this);
}
}