依赖**重点内容**
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'org.xutils:xutils:3.5.0'
**APP**
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
inite();
}
private void inite() {
x.Ext.init(this);
}
}
**Mainactivity**
public class MainActivity extends AppCompatActivity implements accountView{
private accountPresenter presenter;
@ViewInject(R.id.ed_name)
private EditText ed_name;
@ViewInject(R.id.ed_pwd)
private EditText ed_pwd;
private String mobile,pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x.view().inject(this);
presenter=new accountPresenter(this);
}
@Event({R.id.login,R.id.zhu})
private void accountCallback(View view){
mobile = ed_name.getText().toString();
pwd= ed_pwd.getText().toString();
switch (view.getId()){
case R.id.zhu:
if(TextUtils.isEmpty(mobile)||TextUtils.isEmpty(pwd)){
Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
}else {
presenter.register(mobile,pwd);
}
break;
case R.id.login:
presenter.login(mobile,pwd);
break;
}
}
@Override
public void ShowSuccess(final String msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void ShowError(final String msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
}
});
}
}
View**重点内容**
public interface accountView {
void ShowSuccess(String msg);
void ShowError(String msg);
}
P**重点内容**
public class accountPresenter {
private accountView view;
private accountModel model;
public accountPresenter(accountView view) {
this.view = view;
model = new accountModel();
}
public void register(String mobile, String pwd) {
model.register(mobile, pwd, new accountModel.accountCallback() {
@Override
public void onSuccess() {
view.ShowSuccess("注册成功");
}
@Override
public void onError(String errormsg) {
view.ShowError("注册失败");
}
});
{
}
}
public void login(String mobile, String pwd) {
model.login(mobile, pwd, new accountModel.accountCallback() {
@Override
public void onSuccess() {
view.ShowSuccess("登陆成功");
}
@Override
public void onError(String errormsg) {
view.ShowError("登录失败");
}
});
}
}
M**重点内容**
public class accountModel {
private OkHttpClient client;
public accountModel(){
client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
}
public void register(String mobile, String pwd, final accountCallback callback) {
FormBody body = new FormBody.Builder()
.add("name", mobile)
.add("pwd", pwd)
.build();
Request build = new Request.Builder()
.url("https://www.zhaoapi.cn/user/reg")
.post(body)
.build();
client.newCall(build).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callback.onError(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
callback.onSuccess();
}
});
}
public void login(String mobile, String pwd, final accountCallback callback) {
FormBody body = new FormBody.Builder()
.add("name",mobile)
.add("pwd",pwd)
.build();
Request build = new Request.Builder()
.url("https://www.zhaoapi.cn/user/login")
.post(body)
.build();
client.newCall(build).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callback.onError(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
callback.onSuccess();
}
});
}
public interface accountCallback{
void onSuccess();
void onError(String errormsg);
}
}
登陆注册
最新推荐文章于 2024-07-19 05:15:00 发布