mvp


/**
 * @Author: zhang
 * @Date: 2019/3/21 8:45
 * @Description:M层
 */
public class MyModel implements MyInterface.ModelInterface {
    MyCallBack myCallBack;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String json = (String) msg.obj;
            if (msg.arg1 == 1){
                try {
                    JSONObject object = new JSONObject(json);
                    String m = object.getString("message");
                    myCallBack.success(m);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }else {
                Gson gson = new Gson();
                ShopBean bean = gson.fromJson(json,ShopBean.class);
                myCallBack.success(bean);
            }
        }
    };
    @Override
    public void PostRequest(String url,String phone,String pwd,MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
        OkHttpUtil.getInstance().doPost(url, phone, pwd, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Message message = new Message();
                message.obj = json;
                message.arg1 = 1;
                handler.sendMessage(message);
            }
        });
    }

    @Override
    public void toRequest(String url, MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
        OkHttpUtil.getInstance().doGet(url,handler);
    }

    public interface MyCallBack{
        public void success(Object obj);
        public void error(Object obj);
    }
}
/**
 * @Author: zhang
 * @Date: 2019/3/21 8:48
 * @Description:P层
 */
public class MyPresenter<T> implements MyInterface.PresenterInterface {
    //声明Model接口对象
    MyInterface.ModelInterface modelInterface;
    //声明View接口对象
    MyInterface.ViewInterface viewInterface;
    MyInterface.ViewInterface.LoginInterface loginInterface;
    T tt;
    String lUrl = "http://172.17.8.100/small/commodity/v1/findCommodityByKeyword";
    public MyPresenter(T tt) {
        modelInterface = new MyModel();
        this.tt = tt;
    }

    @Override
    public void toRegister(String phone,String pwd) {
        viewInterface = (MyInterface.ViewInterface) tt;
        modelInterface.PostRequest("http://172.17.8.100/small/user/v1/register",phone,pwd, new MyModel.MyCallBack() {
            @Override
            public void success(Object obj) {
                viewInterface.showRegister((String) obj);
            }

            @Override
            public void error(Object obj) {

            }
        });
    }

    @Override
    public void toLogin(String phone,String pwd) {
        viewInterface = (MyInterface.ViewInterface) tt;
        modelInterface.PostRequest("http://172.17.8.100/small/user/v1/login",phone,pwd, new MyModel.MyCallBack() {
            @Override
            public void success(Object obj) {
                viewInterface.showLogin((String) obj);
            }

            @Override
            public void error(Object obj) {

            }
        });
    }

    @Override
    public void toModel(String key,int page) {
        Log.i("tag","P"+key);
        loginInterface = (MyInterface.ViewInterface.LoginInterface) tt;
        String dataUrl = lUrl +"?count=10&page="+page+"&keyword="+key;
        modelInterface.toRequest(dataUrl, new MyModel.MyCallBack() {
            @Override
            public void success(Object obj) {
                if (obj instanceof ShopBean){
                    ShopBean bean = (ShopBean) obj;
                    loginInterface.reFreDisplay(bean.getResult());
                }
            }

            @Override
            public void error(Object obj) {

            }
        });
    }


}
/**
 * @Author: zhang
 * @Date: 2019/3/21 8:54
 * @Description:工具类
 */
public class OkHttpUtil {
    OkHttpClient okHttpClient;
    public static OkHttpUtil okHttpUtil;
    //构造方法私有化
    private OkHttpUtil() {
        okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyInterceptor()).build();
    }

    public static synchronized OkHttpUtil getInstance(){
        if (okHttpUtil == null){
            okHttpUtil = new OkHttpUtil();
        }
        return okHttpUtil;
    }
    public void doPost(String url, String phone, String pwd, Callback callback){
        //FormBody.Builder用来封装参数
        FormBody.Builder build = new FormBody.Builder();
        build.add("phone",phone);
        build.add("pwd",pwd);
        RequestBody body = build.build();
        Request request = new Request.Builder().url(url).post(body).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }
    public void doGet(String url, final Handler mHandler){
        Request request = new Request.Builder().url(url).get().build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Message message = new Message();
                message.obj = json;
                mHandler.sendMessage(message);
            }
        });
    }
    private class MyInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Log.i("tag","url = "+request.url());
            Response proceed = chain.proceed(request);
            return proceed;
        }
    }
}
V层
public class LoginActivity extends AppCompatActivity implements MyInterface.ViewInterface.LoginInterface {

    List<ShopBean.ResultBean> list = new ArrayList<>();
    MyInterface.PresenterInterface presenterInterface;
    XRecyclerView xRecyclerView;
    MyAdapter adapter;
    int page = 1;
    String key = "高";
    MyLineLayout myLineLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        presenterInterface = new MyPresenter(this);
        init();
    }

    private void init()  {
        xRecyclerView = findViewById(R.id.xRecycler_id);
        myLineLayout = findViewById(R.id.myLineLayout);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(layoutManager);
        adapter = new MyAdapter(list,this);
        xRecyclerView.setAdapter(adapter);
        Log.i("tag","V"+key);
        presenterInterface.toModel(key,page);
        xRecyclerView.setLoadingMoreEnabled(true);
        xRecyclerView.setPullRefreshEnabled(true);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                list.clear();
                page = 1;
                presenterInterface.toModel(key,page);
                xRecyclerView.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                page++;
                presenterInterface.toModel(key,page);
                xRecyclerView.loadMoreComplete();
            }
        });
    }

    @Override
    public void reFreDisplay(List<ShopBean.ResultBean> vList) {

        list.addAll(vList);
        Log.i("tag","个数"+list.size());
        adapter.notifyDataSetChanged();
    }


}
登录注册
public class ViewActivity extends AppCompatActivity implements MyInterface.ViewInterface, View.OnClickListener {

    EditText edit_phone,edit_pwd;
    Button but_login,but_register;
    ImageView imageView;
    MyInterface.PresenterInterface presenterInterface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);
        presenterInterface = new MyPresenter(this);
        init();
    }

    private void init() {
        if(Build.VERSION.SDK_INT>=23){
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this,mPermissionList,123);

        }

        imageView = findViewById(R.id.qq_id);
        edit_phone = findViewById(R.id.phone_id);
        edit_pwd = findViewById(R.id.pwd_id);
        but_login = findViewById(R.id.but_login_id);
        but_register = findViewById(R.id.but_register_id);
        but_login.setOnClickListener(this);
        but_register.setOnClickListener(this);
        imageView.setOnClickListener(this);

    }

    @Override
    public void showRegister(String str) {
        Toast.makeText(this,str,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showLogin(String str) {
        if (str.equals("登录成功")){
            Intent intent = new Intent(ViewActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //登录
            case R.id.but_login_id:
                String lPhone = edit_phone.getText().toString();
                String lPwd = edit_pwd.getText().toString();
                presenterInterface.toLogin(lPhone,lPwd);
                break;
            //注册
            case R.id.but_register_id:
                //获取输入框输数据
                String rPhone = edit_phone.getText().toString();
                String rPwd = edit_pwd.getText().toString();
                presenterInterface.toRegister(rPhone,rPwd);
                break;
            case R.id.qq_id:
                UMShareAPI umShareAPI = UMShareAPI.get(this);
                umShareAPI.getPlatformInfo(this, SHARE_MEDIA.QQ, new UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        //开始登录
                        Toast.makeText(ViewActivity.this,"开始登录",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                        //登录成功
                        Toast.makeText(ViewActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(ViewActivity.this,LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                        //登录失败
                        Toast.makeText(ViewActivity.this,"登录失败",Toast.LENGTH_LONG).show();
                        Log.i("tag","登录失败");
                        Log.i("tag",""+throwable);
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {

                    }
                });
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值