MVP接口回调的方式实现
//View
public class ZhuChe extends AppCompatActivity implements IManintenter {
private Button zcbutton;
private EditText name;
private EditText pwd;
private String path = "http://172.17.8.100/small/user/v1/register";
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhu_che);
zcbutton = findViewById(R.id.zcbutton);
name = findViewById(R.id.zcname);
pwd = findViewById(R.id.zcpwd);
final ZCPresenter zcPresenter = new ZCPresenter(this);
zcbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sname = name.getText().toString();
String spwd = pwd.getText().toString();
zcPresenter.getpresenter(path, sname, spwd);
}
});
}
@Override
public void getview(String s) {
Log.i("AA", "注册" + s);
Gson gson = new Gson();
ZCbean zCbean = gson.fromJson(s, ZCbean.class);
if (zCbean.getStatus().equals("0000")) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(ZhuChe.this, "注册成功", Toast.LENGTH_SHORT).show();
}
});
Intent intent = new Intent(ZhuChe.this, MainActivity.class);
startActivity(intent);
} else if (zCbean.getStatus().equals("1001")) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(ZhuChe.this, "账号已注册,或格式错误", Toast.LENGTH_SHORT).show();
}
});
}
}
}
网络请求用OKHttp
// Presenter
public class ZCPresenter {
private final Okhttp getdanli;
ZhuChe zhuChe;
public ZCPresenter(ZhuChe zhuChe1) {
this.zhuChe = zhuChe1;
getdanli = Okhttp.getdanli();
}
public void getpresenter(String s, String name, String pwd) {
getdanli.post(s, name, pwd, new Okhttp.getokhttp() {
@Override
public void getcheng(String s) {
zhuChe.getview(s);
}
});
}
}