MVP登录注册------di包--------依赖以及权限

```implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    compile 'cn.yipianfengye.android:zxing-library:2.2'
------------------------------------------------------
<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>



===============================HttpUtils=================================
package com.example.dell.zk2.di;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by Dell on 2018/9/9.
 */

public class HttpUtils {
    private static HttpUtils httpUtils;
    private OkHttpClient okHttpClient;
    //有参
    private HttpUtils(){
        okHttpClient = new OkHttpClient();
    }

    //单例
    public static HttpUtils getinstance(){
        if(httpUtils==null){
            //同步锁
            synchronized (HttpUtils.class){
                if (httpUtils==null){
                    httpUtils=new HttpUtils();
                }
            }
        }
        return httpUtils;
    }

    public void getdata(String path, Callback callback){
        //创建一个request对象
        Request request = new Request.Builder().url(path).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }

    //post封装
    public void postdata(String path, FormBody formBody,Callback callback){
        Request request = new Request.Builder().method("POST",formBody).url(path).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }

}
===============================moudleimp =================================
package com.example.dell.zk2.di;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Response;

/**
 * Created by Dell on 2018/9/9.
 */

public class moudleimp implements icontract.imoudle{
    private String path ="https://www.zhaoapi.cn/user/reg";

    @Override
    public void requestdata(String phone1, String password1, final callisten callisten) {
        if(phone1.equals("")||password1.equals("")){
            callisten.responsemsg("用户名跟密码不能为空");
            return;
        }

        FormBody build = new FormBody.Builder()
                .add("mobile","")
                .add("paddword","")//提交参数
                .build();
        HttpUtils httpUtils = HttpUtils.getinstance();
        httpUtils.postdata(path, build, new Callback() {

            //失败
            @Override
            public void onFailure(Call call, IOException e) {
                callisten.responsemsg(e.getMessage());
            }

            //成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                callisten.responsemsg("注册成功");
            }
        });
    }


    //登录
    @Override
    public void requestdata1(String phone2, String password2, final callisten callisten) {
        FormBody build = new FormBody.Builder()
                .add("mobile","")
                .add("paddword","")//提交参数
                .build();
        HttpUtils httpUtils = HttpUtils.getinstance();
        httpUtils.postdata(path, build, new Callback() {

            //失败
            @Override
            public void onFailure(Call call, IOException e) {
                callisten.responsemsg(e.getMessage());
            }

            //成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                callisten.responsemsg("登录成功");
            }
        });
    }
}

===============================presenterimp =================================
package com.example.dell.zk2.di;

import java.lang.ref.WeakReference;

/**
 * Created by Dell on 2018/9/9.
 */

public class presenterimp implements icontract.ipresenter<icontract.iview>{
    private icontract.iview iview;
    private WeakReference<icontract.iview> iviewWeakReference;
    private WeakReference<icontract.imoudle> imoudleWeakReference;
    private com.example.dell.zk2.di.moudleimp moudleimp;

    @Override
    public void attachview(icontract.iview iview) {
        this.iview=iview;
        moudleimp = new moudleimp();
        //解决内存泄漏----通过弱引用
        iviewWeakReference = new WeakReference<>(iview);
        //解决M层内存泄漏
        imoudleWeakReference = new WeakReference<icontract.imoudle>(moudleimp);
    }

    @Override
    public void datachview(icontract.iview iview) {
       iviewWeakReference.clear();
       imoudleWeakReference.clear();
    }



    //请求M层
    @Override
    public void requestinfe(String phone1, String password1) {
        moudleimp.requestdata(phone1,password1,new icontract.imoudle.callisten() {
            @Override
            public void responsemsg(String message) {
                iview.showdata(message);
            }
        });
    }


    //登录
    @Override
    public void requestinfe1(String phone2, String password2) {
        moudleimp.requestdata1(phone2,password2,new icontract.imoudle.callisten() {
            @Override
            public void responsemsg(String message) {
                iview.showdata(message);
            }
        });
    }
}




===============================icontract=================================
package com.example.dell.zk2.di;

/**
 * Created by Dell on 2018/9/9.
 */

public interface icontract {


    /*
    * 契约
    * */

    public interface iview{
        void showdata(String message);
    }

    //iview  泛型
    public interface ipresenter<iview>{
        //关联视图
        void attachview(iview iview);
        //取消关联视图
        void datachview(iview iview);
        //注册
        void requestinfe(String phone1, String password1);

        //登录
        void requestinfe1(String phone2, String password2);
    }


    public interface imoudle{
        //接口回调
        public interface callisten{
            //信息请求
            void responsemsg(String message);
        }
        //注册的方法
        void requestdata(String phone1, String password1, callisten callisten);


        //登录的方法
        void requestdata1(String phone2, String password2, callisten callisten);
    }
}

“`

猜你喜欢

转载自blog.csdn.net/qq_42859231/article/details/82557181
今日推荐