登录注册加展示

主页面(登录页面)

package com.example.mvpstudy;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mvpstudy.presenter.LoginPresenter;
import com.example.mvpstudy.utils.OkHttpUtils;
import com.example.mvpstudy.view.LoginView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,LoginView {

    private Button login;
    private EditText phone;
    private EditText pwd;
    private TextView tv_name;
    private LoginPresenter presenter;
    private Button reg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //找控件
        reg = findViewById(R.id.reg);
        login = findViewById(R.id.login);
        phone = findViewById(R.id.phone);
        pwd = findViewById(R.id.pwd);
        tv_name = findViewById(R.id.tv_name);

        //实例化presenter
        presenter = new LoginPresenter(this);
        //点击登录注册
        login.setOnClickListener(this);
        reg.setOnClickListener(this);

    }

    //先获取输入框的值
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                String et_phone = phone.getText().toString();
                String et_pwd = pwd.getText().toString();
                boolean mobileNO = OkHttpUtils.isMobileNO(et_phone);
                if (!mobileNO){
                    Toast.makeText(this, "手机号格式不对", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (et_pwd.length()<3){
                    Toast.makeText(this, "密码不对", Toast.LENGTH_SHORT).show();
                    return;
                }
                //传参
                presenter.sendParameter(et_phone,et_pwd);
                break;
            case R.id.reg:
                Intent intent = new Intent(MainActivity.this, RegActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void view(String nickName) {
        Log.i("xxx",nickName);
        tv_name.setText(nickName);
        Intent intent = new Intent(MainActivity.this, ShowAvtivity.class);
        startActivity(intent);
    }
}

注册页面


    package com.example.mvpstudy;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.mvpstudy.presenter.RegPresenter;
import com.example.mvpstudy.utils.OkHttpUtils;
import com.example.mvpstudy.view.RegView;

public class RegActivity extends AppCompatActivity implements View.OnClickListener,RegView {

    private EditText zhu_phone;
    private EditText zhu_pwd;
    private Button zhu_reg;
    private String phone;
    private String pwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reg_layout);

        zhu_phone = findViewById(R.id.zhu_phone);
        zhu_pwd = findViewById(R.id.zhu_pwd);
        zhu_reg = findViewById(R.id.zhu_reg);
        zhu_reg.setOnClickListener(this);



    }

    @Override
    public void onClick(View v) {
        phone = zhu_phone.getText().toString();
        pwd = zhu_pwd.getText().toString();
        boolean mobileNO = OkHttpUtils.isMobileNO(phone);
        if (mobileNO){
            if (pwd.length()<3){
                Toast.makeText(this, "密码不对", Toast.LENGTH_SHORT).show();
                return;
            }else {
                RegPresenter regPresenter = new RegPresenter(this);
                regPresenter.send(phone,pwd);
            }
        }else {
            Toast.makeText(this, "手机号不对", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent(RegActivity.this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void onMessage(String message) {
        if (message.equals("0000")){
            //跳转
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(RegActivity.this, MainActivity.class);
            startActivity(intent);
            return;
        }else if (message.equals("1001")){
            Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
            return;
        }
    }
}


展示页面

package com.example.mvpstudy;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.example.mvpstudy.adapter.MyAdapter;
import com.example.mvpstudy.presenter.ShowPresenter;
import com.example.mvpstudy.view.ShowView;

import org.json.JSONArray;

public class ShowAvtivity extends AppCompatActivity implements ShowView {

    private RecyclerView rlv;
    private ShowPresenter presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two);

        rlv = findViewById(R.id.rlv);

        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);

        rlv.setLayoutManager(linearLayoutManager);

        presenter = new ShowPresenter(this);
        presenter.related();

    }

    @Override
    public void show(JSONArray result) {
        Log.i("yyyyy",result+"");
        MyAdapter myAdapter=new MyAdapter(this,result);
        rlv.setAdapter(myAdapter);
    }
}

Login的P层

package com.example.mvpstudy.presenter;

import android.util.Log;

import com.example.mvpstudy.model.LoginModel;
import com.example.mvpstudy.view.LoginView;

public class LoginPresenter {

    private final LoginModel loginModel;
    private final LoginView loginView;

    //在构造方法中实例化MOdel对象
    public LoginPresenter(LoginView view) {
        loginModel = new LoginModel();
        loginView = view;
    }

    //传过来的参数
    public void sendParameter(String et_phone, String et_pwd) {
        loginModel.login(et_phone,et_pwd);

        //接收从model回调过来的数据给View
        loginModel.setOnLoginLisenter(new LoginModel.OnLoginLisenter() {
            @Override
            public void onResult(String nickName) {
                Log.i("xxx",nickName);

                //把数据给view
                loginView.view(nickName);
            }
        });
    }
}

Reg的P层

package com.example.mvpstudy.presenter;

import com.example.mvpstudy.model.RegModel;
import com.example.mvpstudy.view.RegView;

public class RegPresenter {

    private final RegModel regModel;
    private final RegView regView;

    public RegPresenter(RegView view) {
        regModel = new RegModel();
        regView = view;
    }


    public void send(String phone, String pwd) {
        regModel.reg(phone,pwd);
        regModel.setOnLoginLisenter(new RegModel.OnRegLisenter() {
            @Override
            public void onMessage(String message) {
                regView.onMessage(message);
            }
        });
    }
}

Show的P层

package com.example.mvpstudy.presenter;

import com.example.mvpstudy.ShowAvtivity;
import com.example.mvpstudy.model.ShowModel;
import com.example.mvpstudy.view.ShowView;

import org.json.JSONArray;

public class ShowPresenter {


    private final ShowModel showModel;
    private final ShowView showView;

    public ShowPresenter(ShowView view) {
        showModel = new ShowModel();
        showView = view;
    }

    //p关联m
    public void related() {
        showModel.getGoodsData();
       showModel.setOnShowLisenter(new ShowModel.OnShowLisenter() {
           @Override
           public void onShow(JSONArray result) {

               showView.show(result);
           }
       });
    }
}

Login的M层

package com.example.mvpstudy.model;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

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.RequestBody;
import okhttp3.Response;

public class LoginModel {

    //定义接口
    public interface OnLoginLisenter{
        void onResult(String nickName);
    }
    //声明接口
    public  OnLoginLisenter loginLisenter;
//提供一个公共的设置监听的方法
    public void setOnLoginLisenter(OnLoginLisenter loginLisenter){
        this.loginLisenter=loginLisenter;
    }


    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    String json= (String) msg.obj;
                    try {
                        JSONObject jsonObject = new JSONObject(json);
                        JSONObject result = jsonObject.getJSONObject("result");
                        String nickName = result.getString("nickName");

                        if (loginLisenter!=null){
                            //回调数据
                            loginLisenter.onResult(nickName);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
            }
        }
    };

    public void login(String et_phone, String et_pwd) {
        //创建网络请求对象
        OkHttpClient okHttpClient=new OkHttpClient.Builder()
                .connectTimeout(5000,TimeUnit.MILLISECONDS)

                .build();
//创建post请求
        RequestBody requestBody=new FormBody.Builder()
                .add("phone",et_phone)
                .add("pwd",et_pwd)
                .build();

        Request request=new Request.Builder()
                .url("http://172.17.8.100/small/user/v1/login")
                .post(requestBody)
                .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();
                Log.i("xxx",json);

                Message message=new Message();
                message.what=0;
                message.obj=json;
                handler.sendMessage(message);
            }
        });
    }
}

Reg的M层

package com.example.mvpstudy.model;

import android.os.Handler;
import android.os.Message;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

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

public class RegModel {
    public interface OnRegLisenter{
        void onMessage(String status);
    }
    private OnRegLisenter onRegLisenter;
    public void setOnLoginLisenter(RegModel.OnRegLisenter onRegLisenter){
        this.onRegLisenter=onRegLisenter;
    }

    public void reg(String phone, String pwd) {
        String registUrl="http://172.17.8.100/small/user/v1/register";

        OkHttpClient okHttpClient = new OkHttpClient();
        FormBody formBody = new FormBody.Builder()
                .add("phone", phone)
                .add("pwd", pwd)
                .build();
        Request request = new Request.Builder()
                .url(registUrl)
                .post(formBody)
                .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.what=0;
                message.obj=json;

            }
        });
    }

    public Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    String obj= (String) msg.obj;
                    try {
                        JSONObject jsonObject = new JSONObject(obj);
                        String message = jsonObject.getString("status");
                        if (onRegLisenter!=null){
                            onRegLisenter.onMessage(message);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };
}

Show的M层

package com.example.mvpstudy.model;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.mvpstudy.utils.OkUtilsHttp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

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

public class ShowModel {

    private String url="http://172.17.8.100/small/commodity/v1/bannerShow";

    public interface OnShowLisenter{
        void onShow(JSONArray result);
    }
    private OnShowLisenter lisenter;
    public void setOnShowLisenter(OnShowLisenter lisenter){
        this.lisenter=lisenter;
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    String json= (String) msg.obj;
                    try {
                        JSONObject jsonObject = new JSONObject(json);
                        JSONArray result = jsonObject.getJSONArray("result");
                        if (lisenter!=null){
                            lisenter.onShow(result);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };
    public void getGoodsData() {
       OkUtilsHttp.getInstance().doGet(url, 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();
               Log.i("yyy",json);
               Message message=new Message();
               message.what=0;
               message.obj=json;
               handler.sendMessage(message);
           }
       });
    }
}

依次为Login的V层,Reg的V层,Show的V层

public interface LoginView {
    void view(String nickName);
}
public interface RegView {
        void onMessage(String message);
}

public interface ShowView {
        void show(JSONArray result);
}

适配器

package com.example.mvpstudy.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.mvpstudy.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    JSONArray result;


    public MyAdapter(Context context, JSONArray result){
        this.result=result;
        this.context=context;
    }



    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,null,false);

        MyViewHolder viewHolder=new MyViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        try {
            JSONObject jsonObject = result.getJSONObject(i);
            String title = jsonObject.getString("title");
            String imageUrl = jsonObject.getString("imageUrl");
            Toast.makeText(context, "123"+title, Toast.LENGTH_SHORT).show();
            myViewHolder.title.setText(title);
            Glide.with(context).load(imageUrl).into(myViewHolder.img);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return result.length();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        private final ImageView img;
        private final TextView title;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            img = itemView.findViewById(R.id.img);
            title = itemView.findViewById(R.id.stitle);
        }
    }
}

判断电话号码

package com.example.mvpstudy.utils;

import android.text.TextUtils;

public class OkHttpUtils {
    public static boolean isMobileNO(String mobileNums) {
        /**
         * 判断字符串是否符合手机号码格式
         * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
         * 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186
         * 电信号段: 133,149,153,170,173,177,180,181,189
         * @param str
         * @return 待检测的字符串
         */
        String telRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$";// "[1]"代表下一位为数字可以是几,"[0-9]"代表可以为0-9中的一个,"[5,7,9]"表示可以是5,7,9中的任意一位,[^4]表示除4以外的任何一个,\\d{9}"代表后面是可以是0~9的数字,有9位。
        if (TextUtils.isEmpty(mobileNums))
            return false;
        else
            return mobileNums.matches(telRegex);
    }

}

网络请求

package com.example.mvpstudy.utils;

import android.telecom.Call;
import android.util.Log;

import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
//封装网络工具类
public class OkUtilsHttp {
    private static OkUtilsHttp okUtilsHttp=null;
    private OkUtilsHttp(){

    }
//返回实例的方法
    public static OkUtilsHttp getInstance(){
        if (okUtilsHttp==null){
            synchronized (OkUtilsHttp.class){
                if (okUtilsHttp==null){
                    //同步锁
                    okUtilsHttp=new OkUtilsHttp();
                }
            }
        }
        return okUtilsHttp;
    }
    //执行get请求,get请求封装的方法

    public static void doGet(String url, Callback callback){
        HttpLoggingInterceptor httpLoggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("ccc",message);
            }
        });
        //指定拦截日志模式
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        //创建OKHTTPclient
        OkHttpClient okHttpClient=new OkHttpClient.Builder()
                //添加拦截
                .addInterceptor(httpLoggingInterceptor)
                .build();
        Request request=new Request.Builder()
                .url(url)
                .build();
        okhttp3.Call call = okHttpClient.newCall(request);
        call.enqueue(callback);

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44405056/article/details/87924348