MVP

 

// 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);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值