欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MVP框架的登陆注册

程序员文章站 2022-04-18 17:18:08
...

//登录页面的布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/etMobile"
        android:gravity="center"
        android:layout_marginTop="@dimen/dimen_30dp"
        android:hint="@string/login_hin"
        android:padding="@dimen/dimen_10dp"
        android:textSize="@dimen/dimen_common_20"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/etPassword"
        android:hint="请输入密码"
        android:gravity="center"
        android:layout_marginTop="@dimen/dimen_20dp"
        android:padding="@dimen/dimen_10dp"
        android:textSize="@dimen/dimen_common_20"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />



    <ProgressBar
        android:id="@+id/progressBar"
        android:visibility="invisible"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvLogin"
        android:text="@string/login"
        android:gravity="center"
        android:layout_marginTop="@dimen/dimen_30dp"
        android:padding="@dimen/dimen_10dp"
        android:textSize="@dimen/dimen_common_20"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

//登录页面

public class LoginActivity extends AppCompatActivity implements LoginView {

    private EditText etMobile;
    private EditText etPassword;
    private View tvLogin;
    private ProgressBar progressBar;

private LoginPresenter loginPresenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initViewListener();
        loginPresenter=new LoginPresenter(this);
    }

    private void initViewListener() {
        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2018/11/9 点击登录
                String mobile = etMobile.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                loginPresenter.login(mobile,password);

            }
        });
    }

    private void initView() {
        etMobile = findViewById(R.id.etMobile);
        etPassword = findViewById(R.id.etPassword);
        tvLogin = findViewById(R.id.tvLogin);
        progressBar=findViewById(R.id.progressBar);
    }

    @Override
    public void onSuccess(String result) {
        SharedPreferences sharedPreferences = getSharedPreferences(Constant.APP_CONFIG, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean(Constant.IS_LOGIN,true);
        edit.commit();
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onFailer(final String erroMsg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(LoginActivity.this,erroMsg,Toast.LENGTH_SHORT).show();
            }
        });
    }
}

//Login 的 Model层

/**
 * 登录
 */
public class LoginModule {

    public void login(String mobile, String password, final HttpCallBack callBack) {
        // TODO: 2018/11/9 判断数据合法性
//        callBack.onFailer("数据非法");

        final String url = "http://www.zhaoapi.cn/user/login";

        new Thread() {

            @Override
            public void run() {
                super.run();

                try {
                    URL url1 = new URL(url);
                    HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    int responseCode = connection.getResponseCode();
                    if (responseCode == 200) {
                        InputStream inputStream = connection.getInputStream();
                        String s = CharStreams.toString(new InputStreamReader(inputStream, "utf-8"));
                        Gson gson = new Gson();

                        RootBean rootBean = gson.fromJson(s, RootBean.class);

                        if (rootBean.getCode() == 0) {
                            callBack.onSuccess(rootBean.getData());
                        } else {
                            callBack.onFailer(rootBean.getMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();
        try {
            Thread.sleep(3000);
            if (mobile.equals("chenglong") && password.equals("123456")) {

                callBack.onSuccess("登入成功");
            } else {
                callBack.onFailer("登入失败");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

//网络请求回调

public interface HttpCallBack {

    void onSuccess(String result);
    void onFailer(String erroMsg);
}

Login 的 View层


public interface LoginView {
    void onSuccess(String result);
    void onFailer(String erroMsg);
}

//Login 的 presenter层


public class LoginPresenter {
    private LoginView loginView;
    private LoginModule loginModule;

    public LoginPresenter(LoginView loginView) {
        this.loginView = loginView;
        loginModule = new LoginModule();

    }

    public void login(String mobile, String password) {
        loginModule.login(mobile, password, new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                
                loginView.onSuccess(result);
            }
            @Override
            public void onFailer(String erroMsg) {
                loginView.onFailer(erroMsg);
            }
        });

    }
}

//Login 的bean类


/**
 * 登录返回
 */
public class LoginResponse implements Serializable {
    private String age;

    private String appkey;

    private String appsecret;

    private String createtime;

    private String email;

    private String fans;

    private String follow;

    private String gender;

    private String icon;

    private String latitude;

    private String longitude;

    private String mobile;

    private String money;

    private String nickname;

    private String password;

    private String praiseNum;

    private String token;

    private int uid;

    private String userId;

    private String username;
}

//注册的Model层


public class RegisterModule {

    public void login(String mobile, String password, final HttpCallBack callBack) {
        // TODO: 2018/11/9 判断数据合法性
//        callBack.onFailer("数据非法");

        final String url = "http://www.zhaoapi.cn/user/login";

        new Thread() {

            @Override
            public void run() {
                super.run();

                try {
                    URL url1 = new URL(url);
                    HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    int responseCode = connection.getResponseCode();
                    if (responseCode == 200) {
                        InputStream inputStream = connection.getInputStream();
                        String s = CharStreams.toString(new InputStreamReader(inputStream, "utf-8"));
                        Gson gson = new Gson();

                        RootBean rootBean = gson.fromJson(s, RootBean.class);

                        if (rootBean.getCode() == 0) {
                            callBack.onSuccess(rootBean.getData());
                        } else {
                            callBack.onFailer(rootBean.getMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();
        try {
            Thread.sleep(3000);
            if (mobile.equals("chenglong") && password.equals("123456")) {

                callBack.onSuccess(注册入成功");
            } else {
                callBack.onFailer("注册失败");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

//注册的其余页面等同于登录页面