activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:gravity="center" android:hint="请输入手机号" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:gravity="center" android:hint="请输入密码" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="登录" /> <Button android:id="@+id/reg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="注册" /> </LinearLayout> </LinearLayout>
activity_reg.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:gravity="center" android:hint="请输入手机号" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:gravity="center" android:hint="请输入密码" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="登录" /> <Button android:id="@+id/reg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="注册" /> </LinearLayout> </LinearLayout>
bean包 MyData.java
package com.mvplogin.bean; /** * Created by lenovo on 2017/11/07. */ public class MyData { /** * msg : 登录成功 * code : 0 * data : {"age":null,"appkey":"770f5b35194cb46c","appsecret":"DAF145434D87578718CC32A246C9F50D","createtime":"2017-11-07T18:36:43","email":null,"gender":null,"icon":null,"mobile":"18256189819","money":null,"nickname":null,"password":"8F669074CAF5513351A2DE5CC22AC04C","token":"ACC1909F8EC2AD669F9095D77C063381","uid":1648,"username":"18256189819"} */ private String msg; private 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; } }
model包 LoginModel.java
package com.mvplogin.model; import android.os.Handler; import android.os.Message; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import com.mvplogin.bean.MyData; /** * Created by lenovo on 2017/11/07. */ public class LoginModel { ModelCallPresenter modelCallPresenter; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); modelCallPresenter.success((String) msg.obj); } }; public void Login(final String wz, final String phone, final String password,ModelCallPresenter modelCallPresenter) { this.modelCallPresenter = modelCallPresenter; new Thread(new Runnable() { @Override public void run() { BufferedReader br; try { URL url = new URL("http://120.27.23.105/user/" + wz + "?mobile=" + phone + "&&password=" + password); HttpURLConnection con = (HttpURLConnection) url.openConnection(); int code = con.getResponseCode(); if (code == 200) { br = new BufferedReader(new InputStreamReader(con.getInputStream())); Gson gson = new Gson(); String data = gson.fromJson(br.readLine(), MyData.class).getMsg(); Message m = new Message(); m.obj = data; handler.sendMessage(m); } } catch (Exception e) { e.printStackTrace(); } } }).start(); } public interface ModelCallPresenter { void success(String msg); } }
presenter包 LoginPresenter.java
package com.mvplogin.presenter; import com.mvplogin.model.LoginModel; /** * Created by lenovo on 2017/11/07. */ public class LoginPresenter { MainPresenter mainPresenter; private LoginModel loginModel; public LoginPresenter(MainPresenter mainPresenter){ this.mainPresenter = mainPresenter; loginModel = new LoginModel(); } public void loginClick(String wz,String phone,String password){ loginModel.Login(wz,phone,password, new LoginModel.ModelCallPresenter() { @Override public void success(String msg) { mainPresenter.lClick(msg); } }); } public void regClick(){ mainPresenter.rClick(); } public interface MainPresenter{ void lClick(String msg); void rClick(); } }
view包 MainActivity.java
package com.mvplogin.view; 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.mvplogin.R; import com.mvplogin.presenter.LoginPresenter; public class MainActivity extends AppCompatActivity implements LoginPresenter.MainPresenter{ private EditText phone,password; private Button login,reg; private LoginPresenter loginPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); phone = (EditText) findViewById(R.id.phone); password = (EditText) findViewById(R.id.password); login = (Button) findViewById(R.id.login); reg = (Button) findViewById(R.id.reg); loginPresenter = new LoginPresenter(this); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { loginPresenter.loginClick("login", phone.getText().toString(), password.getText().toString()); } }); reg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { loginPresenter.regClick(); } }); } @Override public void lClick(String msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } @Override public void rClick() { startActivity(new Intent(MainActivity.this, RegActivity.class)); } }
view包 RegActivity.java
package com.mvplogin.view; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.mvplogin.R; import com.mvplogin.presenter.LoginPresenter; /** * Created by lenovo on 2017/11/07. */ public class RegActivity extends AppCompatActivity implements LoginPresenter.MainPresenter{ private EditText rphone,rpassword; private Button lreg; private LoginPresenter loginPresenter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reg); rphone = (EditText) findViewById(R.id.rphone); rpassword = (EditText) findViewById(R.id.rpassword); lreg = (Button) findViewById(R.id.lreg); loginPresenter = new LoginPresenter(this); lreg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { loginPresenter.loginClick("reg", rphone.getText().toString(), rpassword.getText().toString()); } }); } @Override public void lClick(String msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } @Override public void rClick() { } }