MVP实现


public class MainActivity extends AppCompatActivity implements Main{

    private EditText edit_name;
private EditText edit_pwd;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit_name = findViewById(R.id.edit_name);
	edit_pwd = findViewById(R.id.edit_pwd);

	String dengluname = edit_name.getText().toString();
	String denglupwd = edit_pwd.getText().toString();
	//Presenter自定义类  充当中间者
	//MainActivity的 接口 通过Presenter自定义类(Presenter层)传递给model层
	Presenter presenter= new Presenter(this);
	//ApiUtil.denglu地址、路径、网址、接口
	presenter.getDengLuUrl(ApiUtil.denglu,dengluname,denglupwd);
} @Override public void getDengLuBean(final MyDengLuBean myDengLuBean) { runOnUiThread(new Runnable() { @Override public void run() { //吐司解析数据
            }
        });
    }
}
//MainActivity实现接口Main

public interface Main {
    void getDengLuBean(MyDengLuBean myDengLuBean);
}


//Presenter自定义类  充当中间者
public class Presenter implements PresenterPort{
    private Main main;
    private final Model model;

    public Presenter(Main main) {
        model = new Model(this);
        this.main = main;
    }
    @Override
    public void getDengLuBean(MyDengLuBean myDengLuBean) {
        main.getDengLuBean(myDengLuBean);
    }
    public void getDengLuUrl(String denglu, String dengluname, String denglupwd) {
        model.getDengLuUrl(denglu,dengluname,denglupwd);
    }
}
 
//Presenter自定义类实现接口PresenterPort
public interface PresenterPort {
//MyDengLuBean myDengLuBean是bean类  
//也可是String类型的json串如果是json串model返回的是json串
    void getDengLuBean(MyDengLuBean myDengLuBean);
}

public class Model {

    private Presenter presenter;

    public Model(Presenter presenter) {

        this.presenter = presenter;
    }

    public void getDengLuUrl(String denglu, String dengluname, String denglupwd) {
        Map<String, String> params=new HashMap<>();
        params.put("mobile",dengluname);
        params.put("password",denglupwd);
	//OkHttp3Util二次分装参考
	//http://blog.csdn.net/wei844067872/article/details/78778372
	//导入依赖
	compile 'com.google.code.gson:gson:2.8.2'
    	compile 'com.squareup.okhttp3:okhttp:3.6.0'
    	compile 'com.squareup.okio:okio:1.11.0'
        OkHttp3Util.doPost(denglu, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()){
                    String json = response.body().string();
                    MyDengLuBean myDengLuBean = new Gson().fromJson(json, MyDengLuBean.class);
                    presenter.getDengLuBean(myDengLuBean);

                }
            }
        });
    }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值