1. 程式人生 > >乞丐版MVP之登入

乞丐版MVP之登入

聯網工具類

public static String get(String url) {
        try {
            URL url1 = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(2000);
            InputStream inputStream = connection.getInputStream();
            StringBuilder stringBuilder = new StringBuilder();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String temp = "";
            while ((temp = bufferedReader.readLine()) != null) {
                stringBuilder.append(temp);
                temp = "";
            }
            String data = stringBuilder.toString();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

登入檢視層

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

登入模型層

public static User Login(String name ,String password) {
        String data = Utils.get("http://www.xieast.com/api/user/login.php?username=" + name + "&password=" + password);
        Gson gson = new Gson();
        User user = gson.fromJson(data, User.class);
        return user;
    }

登入Persenter層

public class LoginPersenter {
    private LoginView loginView ;

    public LoginPersenter(LoginView loginView) {
        this.loginView = loginView;
    }

    public void Login(final String name, final String password) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                User LoginData = LoginModel.Login(name, password);
                Message message = Message.obtain();
                message.obj = LoginData;
                handler.sendMessage(message);
            }
        }).start();
    };

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            User LoginData = (User) msg.obj;
            int code = LoginData.getCode();
            String msg1 = LoginData.getMsg();
            switch (code) {
                case 100:
                    loginView.onSuccess(msg1);
                    break;
                case 101:
                    loginView.onFailer(msg1);
                    break;
            }
        }
    };
}

登入Activity 一定要implements LoginView 的兩個方法

private SharedPreferences sp;
    private SharedPreferences.Editor edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        persenter = new LoginPersenter(this);
        initView();
        initPassword();
    }



    private void initView() {
        mEditName = (EditText) findViewById(R.id.edit_name);
        mEditPassword = (EditText) findViewById(R.id.edit_password);
        mCheckPassword = (CheckBox) findViewById(R.id.Check_password);
        mCheckAutomatic = (CheckBox) findViewById(R.id.Check_automatic);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mBtnLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String name = mEditName.getText().toString().trim();
                String password = mEditPassword.getText().toString();
                if (mCheckPassword.isChecked()) {
                    edit = sp.edit();
                    edit.putString("name", name);
                    edit.putString("password", password);
                    edit.putBoolean("isChecked", true).commit();
                } else {
                    edit = sp.edit();
                    edit.clear().commit();
                }
                persenter.Login(name,password);
                break;
        }
    }

    private void initPassword() {
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        boolean isChecked = sp.getBoolean("isChecked", false);
        if (isChecked) {
            mCheckPassword.setChecked(true);
            mEditName.setText(sp.getString("name", ""));
            mEditPassword.setText(sp.getString("password", ""));
        }
        mCheckAutomatic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mCheckPassword.setChecked(true);
                }
            }
        });
        mCheckPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    mCheckAutomatic.setChecked(false);
                }
            }
        });
    }

    @Override
    public void onSuccess(String result) {
        Toast.makeText(LoginActivity.this,result,Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        finish();
        startActivity(intent);
    }

    @Override
    public void onFailer(String msg) {
        Toast.makeText(LoginActivity.this,msg,Toast.LENGTH_SHORT).show();
    }