登录注册并且记住密码

//bean层
package com.bwei.www.week021014.bean;



public class LoginBean {
    /**
     * msg : 登录成功
     * code : 100
     * data : {"id":1,"name":"admin","mobile":13800138000}
     */

    private String msg;
    private int code;
    private DataBean data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * id : 1
         * name : admin
         * mobile : 13800138000
         */

        private int id;
        private String name;
        private long mobile;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public long getMobile() {
            return mobile;
        }

        public void setMobile(long mobile) {
            this.mobile = mobile;
        }
    }
}

 

//inter层

package com.bwei.www.week021014.inter;



public interface ICallBack {
    //接口回调
    void onSuccess(Object obj);
    void onFailed(Exception e);
}

//HttpUtils层

package com.bwei.www.week021014.Utils;

import android.os.Handler;

import com.bwei.www.week021014.inter.ICallBack;
import com.google.gson.Gson;

import java.io.IOException;
import java.lang.reflect.Type;

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



public class HttpUtils {
    //单例
    private static volatile HttpUtils instance;

    private OkHttpClient client;

    private Handler handler = new Handler();

    private HttpUtils(){
        client = new OkHttpClient();
    }
    public static HttpUtils getInstance(){
        if (instance == null){
            synchronized (HttpUtils.class){
                if (null == instance){
                    instance = new HttpUtils();
                }
            }

        }
        return instance;
    }
    public void get(String url, final ICallBack callBack, final Type type){
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                    callBack.onFailed(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                final Object o = gson.fromJson(result,type);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onSuccess(o);
                    }
                });

            }
        });

    }

}

//View层

package com.bwei.www.week021014.view;

import android.content.Context;



public interface IView<T> {
    //实现这个接口并且实现里的方法
    void success(T t);

    void failed(Exception e);

    String getUsername();

    String getPassword();

    void setUsername(String username);

    void setPassword(String password);

    void show();

    void dissmiss();

    void gotoMain();

    void check(boolean isChecked);

    Context getContext();

}

//model层

package com.bwei.www.week021014.model;

import com.bwei.www.week021014.Utils.HttpUtils;
import com.bwei.www.week021014.inter.ICallBack;

import java.lang.reflect.Type;



public class LoginModel {
    public void login(String url, ICallBack callBack, Type type){
        //引用Http获取数据 并且解析
        HttpUtils.getInstance().get(url,callBack,type);

    }

}

//Persenter层

package com.bwei.www.week021014.persenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.bwei.www.week021014.bean.LoginBean;
import com.bwei.www.week021014.inter.ICallBack;
import com.bwei.www.week021014.model.LoginModel;
import com.bwei.www.week021014.view.IView;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;



public class LoginPresenter {
//处理数据
    private IView iv;
    private LoginModel loginModel;

    public void attach(IView iv){
        this.iv = iv;
        loginModel = new LoginModel();
    }
    public void detach(){
        if (iv != null){
            iv = null;
        }
    }
//检验
    public void check(){
        if (TextUtils.isEmpty(iv.getUsername())||TextUtils.isEmpty(iv.getPassword())){
iv.check(false);
        }else {
            iv.check(true);
        }

    }
    //判断是否第一次登陆
    public void isFirst(){
        SharedPreferences sp = iv.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        String username = sp.getString("username","");
        String password = sp.getString("password","");

        if (!TextUtils.isEmpty(username)&&!TextUtils.isEmpty(password)){
            iv.setUsername(username);
            iv.setPassword(password);
        }
    }
    public void login(String url){
        iv.show();

        final String username = iv.getUsername();
        final String password = iv.getPassword();

        url= url.concat("?username=").concat(username).concat("&password=").concat(password);

        Type type = new TypeToken<LoginBean>(){}.getType();

        loginModel.login(url, new ICallBack() {
            @Override
            public void onSuccess(Object obj) {
                iv.dissmiss();
                iv.success(obj);

                SharedPreferences sp = iv.getContext().getSharedPreferences("config",Context.MODE_PRIVATE);
                sp.edit().putString("username",username).putString("password",password)
                        .commit();
                LoginBean bean= (LoginBean) obj;
                if(bean.getCode()==100) {
                    iv.gotoMain();
                }
            }

            @Override
            public void onFailed(Exception e) {
                iv.dissmiss();
                iv.failed(e);

            }
        },type);

    }

}

//MainActivity层

package com.bwei.www.week021014;

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

import com.bwei.www.week021014.bean.LoginBean;
import com.bwei.www.week021014.persenter.LoginPresenter;
import com.bwei.www.week021014.view.IView;

public class MainActivity extends AppCompatActivity implements IView<LoginBean>, View.OnClickListener {
    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;

    private LoginPresenter presenter;
    private ProgressDialog pd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btn_login);

        btnLogin.setOnClickListener(this);

        presenter = new LoginPresenter();
        presenter.attach(this);
        presenter.isFirst();
        pd = new ProgressDialog(this);
        pd.setMessage("正在登陆,请稍后");


    }
    public void success(LoginBean loginBean){
        if (loginBean != null){
            Toast.makeText(this,loginBean.getMsg(),Toast.LENGTH_SHORT).show();
        }

    }
public void failed(Exception e){

        Toast.makeText(this,"网络异常"+ e.getMessage(),Toast.LENGTH_SHORT).show();
}

public String getUsername(){
    return etUsername.getText().toString().trim();
}
    public String getPassword() {
        return etPassword.getText().toString();
    }
    public void setUsername(String username) {
        etUsername.setText(username);
    }
    public void setPassword(String password) {
        etPassword.setText(password);
    }
    public void check(boolean isChecked) {
        if (isChecked) {
            // 显示进度条
            presenter.login("http://www.xieast.com/api/user/login.php");
        }
    }

    @Override
    public Context getContext() {
        return this;
    }

    public void show() {
        pd.show();
    }

    @Override
    public void dissmiss() {
        pd.dismiss();
    }

    @Override
    public void gotoMain() {
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                presenter.check();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detach();
        }
    }
}

//布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwei.www.week021014.MainActivity">

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录" />

</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值