1. 依赖
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
implementation 'com.tencent.bugly:crashreport:latest.release'
//其中latest.release指代最新Bugly SDK版本号,也可以指定明确的版本号,例如2.1.9
implementation 'com.tencent.bugly:nativecrashreport:latest.release'
//其中latest.release指代最新Bugly NDK版本号,也可以指定明确的版本号,例如3.0
implementation project(':pullToRefreshlibrary1')
2 . mean.xml
<LinearLayout 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"
android:orientation="vertical"
>
<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="密码" />
<EditText
android:id="@+id/et_rePassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="确认密码" />
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
3......
layou_list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/thumbnail_pic_s"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="100dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher_background" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/text_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="xxxxxxxxxxxxx"
android:textSize="20dp" />
<TextView
android:id="@+id/text_view"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xxxxxxxxxxxxx"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
4.layout_login
<LinearLayout 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"
android:orientation="vertical"
>
<EditText
android:id="@+id/login_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
5.layout_show.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="@+id/edit_text"
android:layout_weight="9"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/sousuo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="搜索"
/>
</LinearLayout>
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:id="@+id/pull_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.bwei.erzhoukaomoni.ui.wethg.myListView
android:id="@+id/Mylist_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>
6.bean 包
public class Bean {
/**
* data : {"collectIds":[],"email":"","icon":"","id":8911,"password":"123456alex","type":0,"username":"alex1234567"}
* errorCode : 0
* errorMsg :
*/
private DataBean data;
private int errorCode;
private String errorMsg;
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public static class DataBean {
/**
* collectIds : []
* email :
* icon :
* id : 8911
* password : 123456alex
* type : 0
* username : alex1234567
7. MainActivity8.
public class MainActivity extends AppCompatActivity implements IContract.IView, View.OnClickListener {
private EditText et_userName;
private EditText et_password;
private EditText et_rePassword;
private Button btn_register;
private IContract.IPersenter<IContract.IView> present;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
present = new PresentImpl();
present.attachView(this);
}
@Override
public void showResponseMsg(final String failureString) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, failureString, Toast.LENGTH_SHORT).show();
if (failureString.equals("注册成功")){
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
}
});
}
private void initView() {
et_userName = (EditText) findViewById(R.id.et_userName);
et_password = (EditText) findViewById(R.id.et_password);
et_rePassword = (EditText) findViewById(R.id.et_rePassword);
btn_register = (Button) findViewById(R.id.btn_register);
btn_register.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_register:
String userName = et_userName.getText().toString();
String Pwd = et_password.getText().toString();
String rePwd = et_rePassword.getText().toString();
//v层触发P层
present.requestInfo(userName,Pwd,rePwd);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
present.detachView(this);
}
}
8.Loginactivity
public class LoginActivity extends AppCompatActivity implements IContract.IView, View.OnClickListener {
private EditText login_name;
private EditText login_password;
private Button btn_login;
private IContract.IPersenter<IContract.IView> present;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_login);
initView();
//链接p层
present = new PresentImpl();
present.attachView(this);
}
@Override
public void showResponseMsg(final String failureString) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this, failureString, Toast.LENGTH_SHORT).show();
if (failureString.equals("登录成功")){
Intent intent = new Intent(LoginActivity.this,ShowActivity.class);
startActivity(intent);
}
}
});
}
private void initView() {
login_name = (EditText) findViewById(R.id.login_name);
login_password = (EditText) findViewById(R.id.login_password);
btn_login = (Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login:
String name = login_name.getText().toString();
String password = login_password.getText().toString();
present.requestLoginInfo(name,password);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
present.detachView(this);
}
}
9.ShowActivity
public class ShowActivity extends AppCompatActivity implements IContract.IView, View.OnClickListener {
private EditText edit_text;
private Button sousuo;
private myListView Mylist_view;
private PullToRefreshScrollView pull_to_refresh;
private IContract.IPersenter<IContract.IView> present;
private int page =1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_show);
initView();
//连接P层
present = new PresentImpl();
present.attachView(this);
String souxia = edit_text.getText().toString();
pull_to_refresh.setMode(PullToRefreshBase.Mode.BOTH);
pull_to_refresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
page=1;
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
page++;
}
});
}
@Override
public void showResponseMsg(String failureString) {
}
private void initView() {
edit_text = (EditText) findViewById(R.id.edit_text);
sousuo = (Button) findViewById(R.id.sousuo);
Mylist_view = (myListView) findViewById(R.id.Mylist_view);
pull_to_refresh = (PullToRefreshScrollView) findViewById(R.id.pull_to_refresh);
sousuo.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sousuo:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
present.detachView(this);
}
}
10.myListView
public class myListView extends ListView {
public myListView(Context context) {
super(context);
}
public myListView(Context context, AttributeSet attrs) {
super(context, attrs,0);
}
public myListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, i);
}
}
di------------------
10.IContract接口
public interface IContract {
public interface IView{
//回显数据
void showResponseMsg(String failureString);
}
public interface IPersenter<IView>{
/**
* 关联
*/
void attachView(IView iView);
/**
* 解绑
*/
void detachView(IView iView);
/**
* 触发请求
* @param userName
* @param pwd
* @param rePwd
*/
void requestInfo(String userName, String pwd, String rePwd);
void requestLoginInfo(String name, String password);
void requesrShowInfo();
}
public interface IModel{
interface onCallBack{
//返回数据
void responseMsg(String failureString);
}
/**
* 请求数据
* @param userName
* @param pwd
* @param rePwd
*/
void requestData(String userName, String pwd, String rePwd,onCallBack onCallBack);
void requestLoginData(String name, String password, onCallBack onCallBack);
void requestShowData(onCallBack onCallBack);
}
// public interface LoginModel{
//
// interface onLoginCallback{
//
// void responseMsg(String failureString);
// }
// void requestLoginData(onLoginCallback onLoginCallback);
// }
}
11.model包下的 Medelimpl类
public class MedelImpl implements IContract.IModel {
public static final String URL_STRING = "http://www.wanandroid.com/user/register";
public static final String URL_LOGIN = "http://www.wanandroid.com/user/login";
@Override
public void requestData(String userName, String pwd, String rePwd, final onCallBack onCallBack) {
if (userName.equals("") || pwd.equals("") || rePwd.equals("")) {
onCallBack.responseMsg("用户名不能为空");
return;
}
FormBody formBody = new FormBody.Builder()
.add("username", userName)
.add("password", pwd)
.add("repassword", rePwd).build();
OkhttpUtils.getInstance().post(URL_STRING, formBody, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String failureString = e.getMessage().toString();
onCallBack.responseMsg(failureString);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String reponseString = response.body().string();
Gson gson = new Gson();
Bean bean = gson.fromJson(reponseString, Bean.class);
int errorCode = bean.getErrorCode();
if (errorCode==0){
onCallBack.responseMsg("注册成功");
}else{
String errorMsg = bean.getErrorMsg();
onCallBack.responseMsg(errorMsg);
}
}
});
}
@Override
public void requestLoginData(String name, String password, final onCallBack onCallBack) {
if (name.equals("") || password.equals("") ) {
onCallBack.responseMsg("用户名或密码不能为空");
return;
}
FormBody formBody = new FormBody.Builder()
.add("username", name)
.add("password", password).build();
OkhttpUtils.getInstance().post(URL_LOGIN, formBody, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String failureString = e.getMessage().toString();
onCallBack.responseMsg(failureString);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String reponseString = response.body().string();
Gson gson = new Gson();
Bean bean = gson.fromJson(reponseString, Bean.class);
int errorCode = bean.getErrorCode();
if (errorCode==0){
onCallBack.responseMsg("登录成功");
}else {
String errorMsg = bean.getErrorMsg();
onCallBack.responseMsg(errorMsg);
}
}
});
}
@Override
public void requestShowData(onCallBack onCallBack) {
}
}
12presenter包下的presenterimpl类
public class PresentImpl implements IContract.IPersenter<IContract.IView> {
IContract.IView iView;
IContract.IModel medel;
private WeakReference<IContract.IView> iViewWeakReference;
private WeakReference<IContract.IModel> iModelWeakReference;
@Override
public void attachView(IContract.IView iView) {
this.iView = iView;
medel = new MedelImpl();
//弱应用包裹v层 和 m层
iViewWeakReference = new WeakReference<>(iView);
iModelWeakReference = new WeakReference<>(medel);
}
@Override
public void detachView(IContract.IView iView) {
iViewWeakReference.clear();
iModelWeakReference.clear();
}
@Override
public void requestInfo(String userName, String pwd, String rePwd) {
medel.requestData(userName, pwd, rePwd, new IContract.IModel.onCallBack() {
@Override
public void responseMsg(String failureString) {
iView.showResponseMsg(failureString);
}
});
}
@Override
public void requestLoginInfo(String name, String password) {
medel.requestLoginData(name,password,new IContract.IModel.onCallBack() {
@Override
public void responseMsg(String failureString) {
iView.showResponseMsg(failureString);
}
});
}
@Override
public void requesrShowInfo() {
medel.requestShowData(new IContract.IModel.onCallBack() {
@Override
public void responseMsg(String failureString) {
}
});
}
}
13.App类
public class App extends Application {
private static App app;
@Override
public void onCreate() {
super.onCreate();
app = this;
CrashReport.initCrashReport(getApplicationContext(),"sds4f5ds4fsd",false);
}
public static App getInstance(){
return app;
}
}
14.OkhttpUtils类
public class OkhttpUtils {
static OkhttpUtils okhttpUtils;
OkHttpClient okHttpClient;
private PersistentCookieJar cookieJar;
private OkhttpUtils(){
if (null == okHttpClient) {
synchronized (OkhttpUtils.class) {
if (null == okHttpClient) {
// okHttpClient = new OkHttpClient();
cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(App.getInstance()));
okHttpClient = new OkHttpClient.Builder().cookieJar(cookieJar).build();
}
}
}
}
public static OkhttpUtils getInstance(){
if (null == okhttpUtils){
synchronized (OkhttpUtils.class){
if (null == okhttpUtils){
okhttpUtils = new OkhttpUtils();
}
}
}
return okhttpUtils;
}
public void post(String urlstring, FormBody formBody, Callback callback){
Request request = new Request.Builder().method("POST", formBody).url(urlstring).build();
okHttpClient.newCall(request).enqueue(callback);
}
}