登陆注册

依赖**重点内容**
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);
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值