json数据要经过Get恳求获取,这儿有个重要的知识点,get恳求需求拼接url的 本次拼接url的参数为phone,因为登录的时候现已填了手机号,如果这儿再收集手机号就会让客户体会变差,所以我采用了SharedPreferences进行记载并调出

SharedPreferences pref=getSharedPreferences("data",MODE_PRIVATE);
        String phone=pref.getString("phone","");

得到了phone之后,我采用了okhttp恳求回来json,留意:进行网络恳求都需求开启线程以及一些必要操作 例如

<uses-permission android:name="android.permission.INTERNET" />

url为你请求的网络url

 new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client=new OkHttpClient().newBuilder()
                        .connectTimeout(60000, TimeUnit.MILLISECONDS)
                        .readTimeout(60000,TimeUnit.MILLISECONDS).build();
                //url/phone
                Request request=new Request.Builder().url("url/phone"+phone).build();
                try {
                    Response sponse=client.newCall(request).execute();
                    String string = sponse.body().string();
                    Log.d("list",string);
                    jsonJXDate(string);
                }catch (IOException | JSONException e){
                    e.printStackTrace();
                }
            }
        }).start();

由上可知,string即为所需的json

展示大约长这样

{
  "code": 200,
  "message": "成功",
  "data": [
    {
      "id": "string",
      "createTime": "2023-04-18T05:50:08.905+00:00",
      "updateTime": "2023-04-18T05:50:08.905+00:00",
      "isDeleted": 0,
      "param": {},
      "phone": "15019649343",
      "commercialTenant": "string",
      "payTime": "2023-04-18T05:50:08.905+00:00",
      "type": "string",
      "paymentType": "string",
      "bills": [
        {
          "product": "烧烤",
          "amount": "4",
          "price": "60",
          "subtotal": "240"
        }
      ],
      "total": "string"
    },
    {
      "id": "643e9efb09ecf071b0fd2df0",
      "createTime": "2023-04-18T13:28:35.889+00:00",
      "updateTime": "2023-04-18T13:28:35.889+00:00",
      "isDeleted": 0,
      "param": {},
      "phone": "15019649343",
      "commercialTenant": "string",
      "payTime": "2023-04-18T13:28:35.889+00:00",
      "type": "string",
      "paymentType": "string",
      "bills": [
        {
          "product": "兰州拉面",
          "amount": "5",
          "price": "40",
          "subtotal": "200"
        }
      ],
      "total": "string"
    }
  ],
  "ok": true
}

我所需求的是payTime,product,subtotal

有{}用JSONObject,有[]用JSONArray,一步步来靠近你的需求

JSONObject j1 = new JSONObject(data);
            try {
                JSONArray array = j1.getJSONArray("data");
                for (int i=0;i<array.length();i++){
                    j1=array.getJSONObject(i);
                    Map<String,Object>map=new HashMap<>();
                    String payTime = j1.getString("payTime");
                    JSONObject bills = j1.getJSONArray("bills").getJSONObject(0);
                    String product = bills.getString("product");
                    String subtotal = bills.getString("subtotal");
                    map.put("payTime",payTime);
                    map.put("product",product);
                    map.put("subtotal",subtotal);
                    list.add(map);
                }
                Message msg=new Message();
                msg.what=1;
                handler.sendMessage(msg);
            }catch (JSONException e){
                e.printStackTrace();
            }
    }
    public Handler handler=new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    //添加分割线
                    rv.addItemDecoration(new androidx.recyclerview.widget.DividerItemDecoration(
                            MeActivity.this, androidx.recyclerview.widget.DividerItemDecoration.VERTICAL));
                    MyAdapter recy = new MyAdapter(MeActivity.this, list);
                    //设置布局显现格局
                    rv.setLayoutManager(new LinearLayoutManager(MeActivity.this));
                    rv.setAdapter(recy);
                    break;
            }
        }
    };

在adapter处经过惯例layout显现后填入数据

 //界说时间展示格局
        Map<String, Object> map = list.get(position);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        LocalDateTime dateTime = LocalDateTime.parse(map.get("payTime").toString(), formatter);
        String strDate = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        holder.produce.setText(map.get("product").toString());
        holder.payTime.setText(strDate);
        holder.price.setText(map.get("subtotal").toString());

就大功告成啦,因为后台那儿还没把base64图片传上来,导致少了个图片,大致就是这样的

Android 将json数据显示在RecyclerView