A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 不二晨 金牌黑马   /  2019-4-8 14:22  /  567 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

《MVP》

《UserView 》
public interface UserView {
void success(String data);

void fail(String error);
1
}

《UserModel 》
public interface UserModel {

interface CallBackListener {
    void success(String data);

    void fail(String error);
}

//登录
void doLogin(String phone, String pwd, CallBackListener listener);

//注册
void doRegister(String phone, String pwd, CallBackListener listener);

//修改昵称
//
1
2
3
4
5
6
7
8
9
10
11
12
13
14
}

《UserModelIml 》
public class UserModelIml implements UserModel {

//登录
@Override
public void doLogin(String phone, String pwd, final CallBackListener listener) {
    //走注册
    String registerUrl = "http://172.17.8.100/small/user/v1/login";
    FormBody.Builder builder = new FormBody.Builder();
    builder.add("phone", phone);
    builder.add("pwd", pwd);
    new OkHttpUtils().post(registerUrl, builder).result(new OkHttpUtils.HttpListener() {
        @Override
        public void success(String data) {
            listener.success(data);
        }

        @Override
        public void fail(String error) {
            listener.fail(error);
        }
    });
}

//注册
@Override
public void doRegister(String phone, String pwd, final CallBackListener listener) {
    //走注册
    String registerUrl = "http://172.17.8.100/small/user/v1/register";
    FormBody.Builder builder = new FormBody.Builder();
    builder.add("phone", phone);
    builder.add("pwd", pwd);
    new OkHttpUtils().post(registerUrl, builder).result(new OkHttpUtils.HttpListener() {
        @Override
        public void success(String data) {
            listener.success(data);
        }

        @Override
        public void fail(String error) {
            listener.fail(error);
        }
    });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
}

《UserPresenter 》
public interface UserPresenter {

//登录
void doLogin(String phone, String pwd);

//注册
void doRegister(String phone, String pwd);
1
2
3
4
5
}

《UserPresenterIml》
public class UserPresenterIml implements UserPresenter, UserModel.CallBackListener {

private UserView userView;
private UserModel userModel;

public UserPresenterIml(UserModel userModel, UserView userView) {
    this.userModel = userModel;
    this.userView = userView;
}

//登录
@Override
public void doLogin(String phone, String pwd) {
    userModel.doLogin(phone, pwd, this);
}

//注册
@Override
public void doRegister(String phone, String pwd) {
    userModel.doRegister(phone, pwd, this);
}

@Override
public void success(String data) {
    userView.success(data);
}

@Override
public void fail(String error) {
    userView.fail(error);
}

public void destory() {
    if (userModel != null) {
        userModel = null;
    }
    if (userView != null) {
        userView = null;
    }
    System.gc();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
}

《OkHttpUtils 》
public class OkHttpUtils {

private int HTTP_SUCCESS = 1000;

private int HTTP_FAIL = 1001;
private HttpListener mHttpListener;


public OkHttpUtils get(String url) {
    doHttp(url, 0, null);
    return this;
}

public OkHttpUtils post(String url, FormBody.Builder bodyBuilder) {
    doHttp(url, 1, bodyBuilder);
    return this;
}

//网络请求
private void doHttp(String url, int type, FormBody.Builder bodyBuilder) {
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Log.i("intercept", request.url().host());
            return chain.proceed(request);
        }
    }).build();
    Request.Builder btilder = new Request.Builder();
    if (type == 0) {
        btilder.get();
    } else {
        btilder.post(bodyBuilder.build());
    }
    btilder.url(url);
    Request request = btilder.build();

    final Message message = Message.obtain();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            message.obj = e.getMessage();
            message.what = HTTP_FAIL;
            handler.sendMessage(message);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

            message.obj = response.body().string();
            message.what = HTTP_SUCCESS;
            handler.sendMessage(message);
        }
    });

}

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == HTTP_SUCCESS) {
            String data = (String) msg.obj;
            mHttpListener.success(data);
        } else {
            String error = (String) msg.obj;
            mHttpListener.fail(error);
        }

    }
};

//传递接口
public void result(HttpListener mHttpListener) {
    this.mHttpListener = mHttpListener;
}

public interface HttpListener {
    void success(String data);

    void fail(String error);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
}

《mainactivity布局》
<EditText
    android:id="@+id/edit_pass"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入密码" />

<CheckBox
    android:id="@+id/chek_01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="记住密码"/>
<Button
    android:id="@+id/bt_dl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="登录" />

<Button
    android:id="@+id/bt_zc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="注册" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
《MainActivity》
public class MainActivity extends AppCompatActivity implements View.OnClickListener, UserView {

private SharedPreferences sp;
private EditText user_name, user_pass;
private CheckBox checked;
private UserPresenterIml userPresenterIml;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sp = getSharedPreferences("Nam_e", MODE_PRIVATE);
    user_name = (EditText) findViewById(R.id.edit_name);
    user_pass = (EditText) findViewById(R.id.edit_pass);
    checked = (CheckBox) findViewById(R.id.chek_01);
    findViewById(R.id.bt_zc).setOnClickListener(this);
    findViewById(R.id.bt_dl).setOnClickListener(this);
    userPresenterIml = new UserPresenterIml(new UserModelIml(), this);

}

@Override
protected void onResume() {
    super.onResume();
    String editName = sp.getString("edit_name", null);
    if (!TextUtils.isEmpty(editName)) {
        user_name.setText(editName);
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.bt_dl:
            getName();
            break;
        case R.id.bt_zc:
            Intent intent = new Intent(MainActivity.this, TowActivity.class);
            startActivity(intent);
            break;
    }
}

private void getName() {
    String username = user_name.getText().toString().trim();
    String userpass = user_pass.getText().toString().trim();
    if (TextUtils.isEmpty(username)) {
        tost("账户不可为空");
        return;
    }
    if (TextUtils.isEmpty(userpass)) {
        tost("密码不可为空");
        return;
    }
    userPresenterIml.doLogin(username, userpass);

}

private void tost(String mas) {
    Toast.makeText(MainActivity.this, mas, Toast.LENGTH_SHORT).show();
}

@Override
public void success(String data) {
    Intent intent = new Intent(MainActivity.this, ThreeActivity.class);
    startActivity(intent);
    tost(data);
}

@Override
public void fail(String error) {
    tost(error);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
}

《towactivity布局》
<EditText
    android:id="@+id/edit_pass2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入密码" />
<EditText
    android:id="@+id/edit_pass3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请确认密码" />
<TextView
    android:id="@+id/text_dl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="已有账户,立即登录"
    android:gravity="right"/>
<Button
    android:id="@+id/bt_zc2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="注册" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
《TowActivity 》
public class TowActivity extends AppCompatActivity implements View.OnClickListener, UserView {

private EditText editname;
private EditText editpass;
private EditText editpass1;
private Button bt_zc;
private SharedPreferences sp;
private UserPresenterIml mUserPresenterIml;
private String name;
private String pass;
private String passa;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tow);
    sp = getSharedPreferences("Nam_e", MODE_PRIVATE);
    editname = (EditText) findViewById(R.id.edit_name2);
    editpass = (EditText) findViewById(R.id.edit_pass2);
    editpass1 = (EditText) findViewById(R.id.edit_pass3);
    findViewById(R.id.bt_zc2).setOnClickListener(this);
    findViewById(R.id.text_dl).setOnClickListener(this);
    mUserPresenterIml = new UserPresenterIml(new UserModelIml(), this);
}

private void Zhuce() {
    name = editname.getText().toString().trim();
    pass = editpass.getText().toString().trim();
    passa = editpass1.getText().toString().trim();
    if (TextUtils.isEmpty(name)) {
        toste("用户不可为空");
    }
    if (TextUtils.isEmpty(pass)) {
        toste("密码不可为空");
    }
    if (TextUtils.isEmpty(passa)) {
        toste("确认密码不可为空");
    }
    if (!pass.equals(passa)) {
        toste("请确认您的密码");
        return;
    }

    //走注册
    mUserPresenterIml.doRegister(name, pass);
}

private void toste(String mse) {
    Toast.makeText(TowActivity.this, mse, Toast.LENGTH_SHORT).show();

}

@Override
public void onClick(View v) {
    switch (v.getId()) {

        case R.id.bt_zc2:
            Zhuce();
            break;
        case R.id.text_dl:
            finish();
            break;
    }
}

@Override
public void success(String data) {
    sp.edit().putString("edit_name2", name).commit();
    toste(data);
    finish();
}

@Override
public void fail(String error) {
    toste(error);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mUserPresenterIml.destory();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
}
---------------------
【转载,仅作分享,侵删】
作者:偏执青年
原文:https://blog.csdn.net/weixin_44666694/article/details/89059271
版权声明:本文为博主原创文章,转载请附上博文链接!

1 个回复

倒序浏览
奈斯,感谢分享!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马