登录接口的流程:

代码执行,请求发送到服务端,服务端调用数据库,返回数据,登录成功

一、学习OkHttp网址

https://www.jianshu.com/p/d06382fd048c

二、整体代码包

Ok.zip

三、实现步骤:

1.右击,选择Module

image.png

2.选择import gradle project,导入封装包。

image.png

3.找到对应包的位置

image.png

4.完成,显示该包

image.png

5.添加依赖implementation project(‘:wldlib’)

image.png

6.http在27以下都没办法请求,添加布局文件,使http和手机端兼容

image.png

7.在清单文件中添加请求的权限和兼容的权限

image.png

8.写出请求的页面布局

image.png

9.封装接口提供的数据

image.png

(1)封装code值和返回的数据

image.png

(2)使用内部类,封装data数据值

image.png

10.写出请求的方法,控件的绑定

image.png

四、整体代码

1.activity_main.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity"
  8. android:orientation="vertical"
  9. android:gravity="center">
  10. <EditText
  11. android:id="@+id/et_account"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:hint="请输入账号"
  15. />
  16. <EditText
  17. android:id="@+id/et_password"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:hint="请输入密码"
  21. android:layout_marginTop="20dp"
  22. />
  23. <Button
  24. android:id="@+id/btn_login"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:text="登录"
  28. android:layout_marginTop="20dp"
  29. android:background="#4CAF50"
  30. android:textSize="20sp"
  31. />
  32. </LinearLayout>

2.BaseDataBean文件代码

  1. package com.example.ok.bean;
  2. public class BaseDataBean {
  3. private int code;
  4. private String msg;
  5. public int getCode() {
  6. return code;
  7. }
  8. public void setCode(int code) {
  9. this.code = code;
  10. }
  11. public String getMsg() {
  12. return msg;
  13. }
  14. public void setMsg(String msg) {
  15. this.msg = msg;
  16. }
  17. }

3.UserInfoBean文件代码

  1. package com.example.ok.bean;
  2. public class UserInfoBean extends BaseDataBean{
  3. private DataBean data;
  4. public DataBean getData() {
  5. return data;
  6. }
  7. public void setData(DataBean data) {
  8. this.data = data;
  9. }
  10. public static class DataBean{
  11. private UserBean user;
  12. private UnitBean unit;
  13. public UserBean getUser() {
  14. return user;
  15. }
  16. public void setUser(UserBean user) {
  17. this.user = user;
  18. }
  19. public UnitBean getUnit() {
  20. return unit;
  21. }
  22. public void setUnit(UnitBean unit) {
  23. this.unit = unit;
  24. }
  25. public static class UserBean{
  26. private Integer id;
  27. private String username;
  28. private String password;
  29. private String phone;
  30. private String position;
  31. private Integer unit_id;
  32. public Integer getId() {
  33. return id;
  34. }
  35. public void setId(Integer id) {
  36. this.id = id;
  37. }
  38. public String getUsername() {
  39. return username;
  40. }
  41. public void setUsername(String username) {
  42. this.username = username;
  43. }
  44. public String getPassword() {
  45. return password;
  46. }
  47. public void setPassword(String password) {
  48. this.password = password;
  49. }
  50. public String getPhone() {
  51. return phone;
  52. }
  53. public void setPhone(String phone) {
  54. this.phone = phone;
  55. }
  56. public String getPosition() {
  57. return position;
  58. }
  59. public void setPosition(String position) {
  60. this.position = position;
  61. }
  62. public Integer getUnit_id() {
  63. return unit_id;
  64. }
  65. public void setUnit_id(Integer unit_id) {
  66. this.unit_id = unit_id;
  67. }
  68. }
  69. public static class UnitBean{
  70. private Integer id;
  71. private String unitname;
  72. private String declaration;
  73. private String logo;
  74. private Integer is_stop;
  75. public Integer getId() {
  76. return id;
  77. }
  78. public void setId(Integer id) {
  79. this.id = id;
  80. }
  81. public String getUnitname() {
  82. return unitname;
  83. }
  84. public void setUnitname(String unitname) {
  85. this.unitname = unitname;
  86. }
  87. public String getDeclaration() {
  88. return declaration;
  89. }
  90. public void setDeclaration(String declaration) {
  91. this.declaration = declaration;
  92. }
  93. public String getLogo() {
  94. return logo;
  95. }
  96. public void setLogo(String logo) {
  97. this.logo = logo;
  98. }
  99. public Integer getIs_stop() {
  100. return is_stop;
  101. }
  102. public void setIs_stop(Integer is_stop) {
  103. this.is_stop = is_stop;
  104. }
  105. }
  106. }
  107. }

4.MainActivity文件代码

  1. package com.example.ok;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import com.example.ok.bean.UserInfoBean;
  11. import com.test.mldlib.okhttp.OKHttpCallBack;
  12. import com.test.mldlib.okhttp.OKHttpUtils;
  13. import java.io.IOException;
  14. import okhttp3.Call;
  15. import okhttp3.OkHttpClient;
  16. public class MainActivity extends AppCompatActivity {
  17. private EditText et_account;
  18. private EditText et_password;
  19. private TextView tv_date;
  20. private TextView tv_data;
  21. private Button btn_login;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. initView();
  27. }
  28. public void initView(){
  29. et_account=findViewById(R.id.et_account);
  30. et_password=findViewById(R.id.et_password);
  31. btn_login=findViewById(R.id.btn_login);
  32. tv_date=findViewById(R.id.tv_date);
  33. tv_data=findViewById(R.id.tv_data);
  34. btn_login.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. login(et_account.getText().toString().trim(),et_password.getText().toString().trim());
  38. }
  39. });
  40. }
  41. public void login(String account,String password){
  42. OKHttpUtils.newBuilder().url("http://zhdj.yiruoche.com/index.php/user/login?")
  43. .post()
  44. .addParam("phone",account)
  45. .addParam("password",password)
  46. .build()
  47. .enqueue(new OKHttpCallBack<UserInfoBean>() {
  48. @Override
  49. public void onSuccess(UserInfoBean userInfoBean) {
  50. if (userInfoBean.getCode()==1){
  51. Intent intent=new Intent();
  52. intent.setClass(MainActivity.this,HomePageActivity.class);
  53. startActivity(intent);
  54. }
  55. String msg = userInfoBean.getMsg();
  56. Toast.makeText(MainActivity.this, userInfoBean.getMsg(), Toast.LENGTH_SHORT).show();
  57. tv_date.setText(msg);
  58. tv_data.setText(userInfoBean.getData().getUser().getUsername());
  59. }
  60. @Override
  61. public void onError(int code) {
  62. super.onError(code);
  63. Toast.makeText(MainActivity.this, "登录成功0", Toast.LENGTH_SHORT).show();
  64. }
  65. @Override
  66. public void onFailure(Call call, IOException e) {
  67. super.onFailure(call, e);
  68. Toast.makeText(MainActivity.this, "登录成功1", Toast.LENGTH_SHORT).show();
  69. }
  70. });
  71. }
  72. }

5.network_security_config.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <network-security-config>
  3. <base-config cleartextTrafficPermitted="true"/>
  4. </network-security-config>

6.activity_home_page.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".HomePageActivity"
  8. android:orientation="vertical"
  9. android:gravity="center">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="首页"
  14. android:textSize="60sp"
  15. />
  16. </LinearLayout>

7.HomePageActivity文件代码

  1. package com.example.ok;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. public class HomePageActivity extends AppCompatActivity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_home_page);
  9. }
  10. }

8.结果图:
(1)登录前
image.png
(2)登录时的页面跳转
image.png
(3)登录后返回的数据
image.png

五、不导入包的方式

OkHttpClient.zip

六、检查错误

  1. 1.debug方式<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1616483925284-a71344b5-db80-48df-a0ff-3d58b88f8d1a.png#align=left&display=inline&height=1012&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1012&originWidth=1915&size=201792&status=done&style=none&width=1915)<br />2.网页的接口网址,输入用户名和密码<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1616483998630-ef5213b0-e582-4ab5-b48c-9f68c1b0c46f.png#align=left&display=inline&height=988&margin=%5Bobject%20Object%5D&name=image.png&originHeight=988&originWidth=1756&size=365764&status=done&style=none&width=1756)