MVP初级练习 注册登陆展示
V层注册实现类
package com.example.week1lx2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.week1lx2.di.IPresenterImpl;
import com.example.week1lx2.ui.IPresenter;
import com.example.week1lx2.ui.IView;
public class MainActivity extends AppCompatActivity implements IView{
private EditText zc_user;
private EditText zc_pwd;
private Button zc_btn;
private IPresenter iPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iPresenter = new IPresenterImpl(this);
zc_user = findViewById(R.id.zc_user);
zc_pwd = findViewById(R.id.zc_pwd);
zc_btn = findViewById(R.id.zc_btn);
zc_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = zc_user.getText().toString();
String pwd = zc_pwd.getText().toString();
iPresenter.zcRequest(user,pwd);
}
});
}
@Override
public void ReSponseData(final String str) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void OKReSponseData(final String user, final String pwd) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,user+"?"+pwd,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.putExtra("user",user);
intent.putExtra("pwd",pwd);
Log.i("aaa","跳转页面");
startActivity(intent );
}
});
}
@Override
public void ShowReSponseData(String data) {
}
}
V层登陆实现类
package com.example.week1lx2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.week1lx2.di.IPresenterImpl;
import com.example.week1lx2.ui.IView;
public class LoginActivity extends AppCompatActivity implements IView {
private IView iView;
private IPresenterImpl iPresenter;
private EditText dl_user;
private EditText dl_pwd;
private Button dl_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
iPresenter = new IPresenterImpl(LoginActivity.this);
dl_user = findViewById(R.id.dl_user);
dl_pwd = findViewById(R.id.dl_pwd);
dl_btn = findViewById(R.id.dl_btn);
Intent intent = getIntent();
final String user1 = intent.getStringExtra("user");
final String pwd1 = intent.getStringExtra("pwd");
Log.i("aaa","user:"+user1+"pwd:"+pwd1);
dl_user.setText(user1);
dl_pwd.setText(pwd1);
dl_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
iPresenter.dlRequest(user1,pwd1);
}
});
}
@Override
public void ReSponseData(final String str) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this,str,Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void OKReSponseData(final String user, final String pwd) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this,user+"?"+pwd,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, ShowActivity.class);
Log.i("aaa","跳转展示页面");
startActivity(intent );
}
});
}
@Override
public void ShowReSponseData(String data) {
}
}
V层展示实现类
package com.example.week1lx2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.example.week1lx2.adapter.ShowAdapter;
import com.example.week1lx2.bean.GsonBean;
import com.example.week1lx2.di.IPresenterImpl;
import com.example.week1lx2.ui.IView;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
public class ShowActivity extends AppCompatActivity implements IView{
private IPresenterImpl iPresenter;
private ListView list_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
list_layout = findViewById(R.id.list_view);
iPresenter = new IPresenterImpl(this);
iPresenter.showRequest("https://www.zhaoapi.cn/product/searchProducts?keywords=笔记本&page=1");
}
@Override
public void ReSponseData(String str) {
}
@Override
public void OKReSponseData(String user, String pwd) {
}
@Override
public void ShowReSponseData(final String data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
GsonBean gsonBean = gson.fromJson(data, GsonBean.class);
List<GsonBean.DataBean> list = new ArrayList<>();
List<GsonBean.DataBean> data = gsonBean.getData();
list.addAll(data);
ShowAdapter adapter = new ShowAdapter(ShowActivity.this,list);
list_layout.setAdapter(adapter);
}
});
}
}
P层接口实现类
package com.example.week1lx2.di;
import com.example.week1lx2.MainActivity;
import com.example.week1lx2.bean.GsonBean;
import com.example.week1lx2.ui.IModel;
import com.example.week1lx2.ui.IPresenter;
import com.example.week1lx2.ui.IView;
import java.util.List;
public class IPresenterImpl implements IPresenter {
private final IModel iModel;
private IView iView;
public IPresenterImpl(IView iView) {
this.iView=iView;
iModel = new IModelImpl();
}
@Override
public void zcRequest(String user, String pwd) {
iModel.zcRequestData(user, pwd, new IModel.OnCallBack() {
@Override
public void ResponseMsg(String str) {
iView.ReSponseData(str);
}
@Override
public void OkResponseMsg(String user, String pwd) {
iView.OKReSponseData(user,pwd);
}
});
}
@Override
public void dlRequest(String user, String pwd) {
iModel.dlRequestData(user, pwd, new IModel.dlOnCallBack(){
@Override
public void dlResponseMsg(String str) {
iView.ReSponseData(str);
}
@Override
public void dlOkResponseMsg(String user, String pwd) {
iView.OKReSponseData(user,pwd);
}
});
}
@Override
public void showRequest(String s) {
iModel.showRequset(s, new IModel.ShowOnCallBack() {
@Override
public void ReSponseData(String str) {
iView.ShowReSponseData(str);
}
});
}
}
M层接口类
package com.example.week1lx2.ui;
import com.example.week1lx2.bean.GsonBean;
import java.util.List;
public interface IModel {
void zcRequestData(String user, String pwd,OnCallBack onCallBack);
void dlRequestData(String user, String pwd, dlOnCallBack onCallBack);
void showRequset(String s,ShowOnCallBack showOnCallBack);
interface OnCallBack{
void ResponseMsg(String str);
void OkResponseMsg(String user, String pwd);
}
interface dlOnCallBack{
void dlResponseMsg(String str);
void dlOkResponseMsg(String user, String pwd);
}
interface ShowOnCallBack{
void ReSponseData(String str);
}
}
M层接口实现类
package com.example.week1lx2.di;
import android.util.Log;
import com.example.week1lx2.bean.GsonBean;
import com.example.week1lx2.ui.IModel;
import com.example.week1lx2.utils.OkHttpUtil;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Response;
public class IModelImpl implements IModel {
@Override
public void zcRequestData(final String user, String pwd, final OnCallBack onCallBack) {
FormBody formBody = new FormBody.Builder()
.add("mobile","")
.add("password","").build();
if(!user.equals("")&&!pwd.equals(""))
{
Log.i("aaa","注册都不为空");
onCallBack.OkResponseMsg(user,pwd);
}
else {
Log.i("aaa","注册为空");
OkHttpUtil.instrance().zcUrlRequset("https://www.zhaoapi.cn/user/reg",formBody, new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
Log.i("aaa","str:"+str);
onCallBack.ResponseMsg(str);
}
});
}
}
@Override
public void dlRequestData(String user, String pwd, final dlOnCallBack onCallBack) {
if(!user.equals("")&&!pwd.equals("")){
Log.i("aaa","登陆都不为空");
onCallBack.dlOkResponseMsg(user,pwd);
}else {
Log.i("aaa","登陆为空");
OkHttpUtil.instrance().dlUrlRequset("https://www.zhaoapi.cn/user/login ", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
Log.i("aaa","str:"+str);
onCallBack.dlResponseMsg(str);
}
});
}
}
@Override
public void showRequset(String s, final ShowOnCallBack showonCallBack) {
OkHttpUtil.instrance().ShowUrlRequset(s, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
Log.i("aaa","str:"+str);
// Log.i("aaa","data:"+data);
showonCallBack.ReSponseData(str);
}
});
}
}