实体类对象如何转换成JSON字符串类型:

https://blog.csdn.net/qq_32115447/article/details/106786777

一、整体压缩代码包

OkHttp.zip

二、实现步骤

1.添加fastjson依赖

api ‘com.alibaba:fastjson:1.1.46.android’
image.png

2.定义SharedPreferences成员变量

image.png

3.定义SharedPreferences局部变量

image.png

4.将字符串转换成json格式,创建编辑器,翻译,提交,即可保存数据。

image.png
三、整体代码
1.MainActivity文件代码

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