登录

依赖

implementation ‘com.android.support:recyclerview-v7:28.+’
implementation ‘com.squareup.okio:okio:1.5.0’
implementation ‘com.squareup.okhttp3:okhttp:3.2.0’
implementation ‘com.squareup.okhttp3:logging-interceptor:3.4.1’
implementation ‘com.github.bumptech.glide:glide:4.9.0’

布局

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android
xmlns:app=“http://schemas.android.com/apk/res-auto
xmlns:tools=“http://schemas.android.com/tools
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".activity.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="match_parent"
            android:background="@color/colorAccent"
            android:layout_height="50dp"
            android:text="登陆"
            android:gravity="center"
            android:textSize="30dp"/>

    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:hint="请输入手机号"
            />

        <EditText
            android:id="@+id/pass"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:hint="请输入密码"
            />
    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/deng"
            android:layout_marginLeft="80dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登陆"
            android:textSize="30dp"/>

        <Button
            android:id="@+id/zhu"
            android:layout_marginLeft="80dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"
            android:textSize="30dp"/>



    </LinearLayout>
</LinearLayout>

</android.support.constraint.ConstraintLayout>

主页面

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

private EditText name;
private EditText pass;
private LoginPresonter presonter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name = findViewById(R.id.name);
    pass = findViewById(R.id.pass);
   Button deng= findViewById(R.id.deng);
   Button zhu=  findViewById(R.id.zhu);

    presonter = new LoginPresonter(this);
   deng.setOnClickListener(this);
   zhu.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           Intent intent = new Intent(MainActivity.this, Main3Activity.class);
           startActivity(intent);
       }
   });

}

@Override
public void onClick(View v) {
    String name1 = name.getText().toString().trim();
    String pass1 = pass.getText().toString().trim();
    boolean mobileNO = Uitls.isMobileNO(name1);
    if(!mobileNO){
        Toast.makeText(this, "电话号码不正确", Toast.LENGTH_SHORT).show();
          return;
    }
    if (pass1.length()<3){
        Toast.makeText(this, "密码长度不能小于3", Toast.LENGTH_SHORT).show();
        return;
    }

    presonter.sendPareter(name1,pass1);
}

@Override
public void view(String status) {

if (status.equals(“0000”)){
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}

}

}

View

package com.bw.yq.view;

public interface LoginView {
void view(String status);

}

Presenter

package com.bw.yq.presenter;

import android.util.Log;

import com.bw.yq.activity.MainActivity;
import com.bw.yq.model.LoginModel;
import com.bw.yq.view.LoginView;

public class LoginPresonter {

private final LoginModel loginModel;
private final LoginView loginView;

public LoginPresonter(LoginView view){

    loginModel = new LoginModel();
    loginView = view;
}
public void sendPareter(String name1, String pass1) {
 loginModel.login(name1,pass1);
 loginModel.OnLoginLisenter(new LoginModel.OnLoginLisenter() {
     @Override
     public void onResult(String status) {
         loginView.view(status);
     }
 });
}

}

Model

package com.bw.yq.model;

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

import com.bw.yq.R;

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

public class LoginModel {

public interface  OnLoginLisenter{
    void onResult(String status);
}

public   OnLoginLisenter loginLisenter;

public void OnLoginLisenter(OnLoginLisenter loginLisenter){
    this.loginLisenter=loginLisenter;

}
Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 0:
        String path= (String) msg.obj;
                try {
                    JSONObject jsonObject=new JSONObject(path);
                    String status = jsonObject.getString("status");

                    if (loginLisenter!=null){
                        loginLisenter.onResult(status);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
        }

    }
};

private String url="http://172.17.8.100/small/user/v1/login";
public void login(String name1, String pass1) {
    OkHttpClient okHttpClient=new OkHttpClient();

    RequestBody requestBody=new FormBody.Builder().
            add("phone",name1).
            add("pwd",pass1).
            build();
    Request request=new Request.Builder().url(url).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 string = response.body().string();
            Log.i("xxx",string);
            Message message = new Message();
            message.what=0;
            message.obj=string;
            handler.sendMessage(message);
        }
    });
}

}

Utils

package mvp.bw.com.mvpstudy.utils;

import android.text.TextUtils;

public class Utils {

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);
}

}

OkHttpUtils

package com.bw.yq.uitls;

import android.util.Log;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;

public class OkHttpsUitls {

private  static OkHttpsUitls okHttpsUitls=null;

private static OkHttpsUitls okHttpsUitls1;

private OkHttpsUitls(){

}
public static OkHttpsUitls getInstance(){
    if (okHttpsUitls==null)
    {
        synchronized (OkHttpsUitls.class){
            if (okHttpsUitls==null){
                okHttpsUitls1 = new OkHttpsUitls();

            }
        }
    }

    return okHttpsUitls;
}

public static void doGet(String url,Callback callback){

HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
    @Override
    public void log(String message) {
        Log.i("xxx",message);
    }
});

loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient okHttpClient=new OkHttpClient.Builder().addInterceptor(loggingInterceptor).build();
Request request=new Request.Builder().url(url).
        build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值