MVP 登录 数据库

1.依赖

implementation ‘com.google.code.gson:gson:2.8.5’

2.主函数的布局(登录页面)

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

<EditText
    android:id="@+id/login_phone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入手机号"
    android:padding="10dp" />

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

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

3.登录的主函数代码LoginMainActivity

package com.example.day07mvc;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
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.example.day07mvc.bean.User;
import com.example.day07mvc.core.DataCall;
import com.example.day07mvc.dao.Dao;
import com.example.day07mvc.http.Utils;
import com.example.day07mvc.model.LoginModel;
import com.example.day07mvc.presenter.LoginPresenter;

public class LoginMainActivity extends AppCompatActivity implements View.OnClickListener,DataCall {
//继承接口DataCall
    private EditText login_phone;
    private EditText login_pwd;
    private Button login;
//    private LoginModel loginModel;
    private LoginPresenter loginPresenter;

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

        Dao dao = new Dao(this);
        Cursor cursor = dao.selete("user", null, null, null, null, null, null);
        if (cursor.moveToFirst()){
            //判断是否是 第一次进入 从数据库里查看是否有登陆的信息
            Intent intent = new Intent(LoginMainActivity.this,ChengGong.class);
            startActivity(intent);
            finish();
        }


        login_phone = findViewById(R.id.login_phone);
        login_pwd = findViewById(R.id.login_pwd);
        login = findViewById(R.id.login);
        login.setOnClickListener(this);
//        loginModel = new LoginModel(LoginMainActivity.this);
        loginPresenter = new LoginPresenter(this);
    }

    @Override
    public void onClick(View v) {
        String phone = login_phone.getText().toString();
        String pwd = login_pwd.getText().toString();
//        User user=new User(phone,pwd);
        loginPresenter.login(phone,pwd);
    }

    @Override
    public void callback(User data) {
        Toast.makeText(getBaseContext(),data.getCode()+data.getMsg(),Toast.LENGTH_LONG).show();
        //0代表登录成功
            if(data.getCode().equals("0")){
                Intent intent = new Intent(LoginMainActivity.this,ChengGong.class);
                startActivity(intent);
                //添加
                Dao dao = new Dao(LoginMainActivity.this);
                ContentValues values = new ContentValues();
                values.put("phone","111");
                values.put("pwd","222");
                dao.insert("user",null,values);
                finish();
            }
    }
}

4.LoginModel

public class LoginModel {
public static User login(String phone, String pwd){
        String loginData=Utils.get("http://www.zhaoapi.cn/user/login?mobile="+phone+"&password="+pwd);
        Gson gson=new Gson();
        User user = gson.fromJson(loginData, User.class);
        return user;
    }
}

5.LoginPresenter

package com.example.day07mvc.presenter;

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

import com.example.day07mvc.bean.User;
import com.example.day07mvc.core.DataCall;
import com.example.day07mvc.http.Utils;
import com.example.day07mvc.model.LoginModel;

public class LoginPresenter {
    private DataCall Call;
    public LoginPresenter(DataCall dataCall){
        Call=dataCall;
    }

  /*  另一方法 效果同上
  public void setCall(DataCall call) {
        Call = call;
    }*/

    private Handler mhandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            User r= (User) msg.obj;
//            Toast.makeText(get,""+r,Toast.LENGTH_SHORT).show();
            Call.callback(r);//调回页面
        }
    };
    public void login(final String phone, final String pwd){
        new Thread(){
            @Override
            public void run() {
                super.run();
                User loginData = LoginModel.login(phone, pwd);

                Message message = mhandler.obtainMessage();
                message.obj=loginData;
                message.what=1;
                mhandler.sendMessage(message);

            }
        }.start();
    }
}

6.core包里的DataCall接口使数据返回到页面

package com.example.day07mvc.core;

import com.example.day07mvc.bean.User;

public interface DataCall {
    void callback(User data);
}

7.解析字符串Utils

package com.example.day07mvc.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Utils {
    public static String get(String s) {
        try {
            URL url=new URL(s);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            int responseCode = urlConnection.getResponseCode();
            if (responseCode==200){
                InputStream inputStream = urlConnection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder builder=new StringBuilder();
                String str="";
                while ((str=reader.readLine())!=null){
                    builder.append(str);
                }
                return builder.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return "";
    }
}

8.bean里的实体类User

package com.example.day07mvc.bean;

public class User {
    String msg;
    String code;

    public String getMsg() {
        return msg;
    }

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

    public String getCode() {
        return code;
    }

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

9.sqlite创建表

package com.example.day07mvc.sql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Sqlitea extends SQLiteOpenHelper {
    public Sqlitea(Context context) {
        super(context, "user.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table user (phone char(20),pwd char(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

10.dao层

package com.example.day07mvc.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.day07mvc.LoginMainActivity;
import com.example.day07mvc.sql.Sqlitea;

public class Dao {

    private final SQLiteDatabase database;

    public Dao(Context context) {
        Sqlitea sqlitea = new Sqlitea(context);
        database = sqlitea.getWritableDatabase();
    }
    public long insert(String table, String nullColumnHack, ContentValues values){
        return database.insert(table, nullColumnHack, values);
    }
    public long delete(String table, String whereClause, String[] whereArgs){
        return database.delete(table, whereClause, whereArgs);
    }
    public Cursor selete(String table, String[] columns, String selection,
                         String[] selectionArgs, String groupBy, String having,
                         String orderBy){
        return database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
    }
}

11.登录成功返回登录页面的布局

<?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=".ChengGong">

    <Button
        android:id="@+id/tuichu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="tuichu"
        />

</android.support.constraint.ConstraintLayout>

12.登录成功返回登录页面

package com.example.day07mvc;

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

import com.example.day07mvc.dao.Dao;

public class ChengGong extends AppCompatActivity implements View.OnClickListener {

    private Button tuichu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheng_gong);
        tuichu = findViewById(R.id.tuichu);
        tuichu.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Dao dao = new Dao(ChengGong.this);
        dao.delete("user","",new String[]{});
        Intent intent=new Intent(ChengGong.this,LoginMainActivity.class);
        startActivity(intent);
        finish();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43603372/article/details/84863808
今日推荐