MOdle

package zhangyanran201800906.bwie.com.day07.Mvp;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import zhangyanran201800906.bwie.com.day07.Bean.User;

/**
 * Created by 匹诺曹 on 2018/9/6.
 */



public class AccountModel {
    private OkHttpClient client;
    //创建方法供P层调用
    public AccountModel(){
        client = new OkHttpClient
                .Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30,TimeUnit.SECONDS)
                .readTimeout(30,TimeUnit.SECONDS)
                .build();
    }

    public void reg(String name, String password, final AccountCallack callack) {
        FormBody formBody = new FormBody
                .Builder()
                .add("mobile",name)
                .add("password",password)
                .build();
        Request builder = new Request
                .Builder()
                .url("https://www.zhaoapi.cn/user/reg")
                .post(formBody)
                .build();
        client.newCall(builder).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callack.onError(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callack.onSuccess(response.body().string());
            }
        });
    }

    public void login(String name, String password, final AccountCallack callack) {
        FormBody formBody = new FormBody
                .Builder()
                .add("mobile",name)
                .add("password",password)
                .build();
        Request builder = new Request
                .Builder()
                .url("https://www.zhaoapi.cn/user/login")
                .post(formBody)
                .build();
        client.newCall(builder).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                callack.onError(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code()==200){
                    String result = response.body().string();
                    Gson gson = new Gson();
                    User user = gson.fromJson(result, User.class);
                    String code = user.getCode();
                    callack.onSuccess(code);
                }
            }
        });
    }

    //返回接口
    public interface AccountCallack{
        void onSuccess(String msg);
        void onError(String errorMsg);
        //void login_success(String msg);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值