【问题标题】:Android Studio - Java (JSONObject/JSONArray) Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate org.json.JSONObject.toString()Android Studio - Java (JSONObject/JSONArray) 方法抛出“java.lang.StackOverflowError”异常。无法评估 org.json.JSONObject.toString()
【发布时间】:2022-01-13 07:16:37
【问题描述】:

请求帮助,我正在尝试从 API 获取 JSON 响应,响应 not null 并且有点长,因为它具有 Base64 编码的图片数据This is the fragment from the response

这是我用来获取 JSON 响应并将其转换为 JSONArray 的部分代码

        StringRequest reqMasterDataShift = new StringRequest(Request.Method.GET, ActionUrl.LastShift+"/"+siteCode,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try{

                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray shift = jsonObject.getJSONArray("shift");
                        JSONArray shiftPhoto = jsonObject.getJSONArray("shift_photo"); // Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate org.json.JSONObject.toString()

                        Timestamp currentTime = new java.sql.Timestamp(System.currentTimeMillis());
                        ObjectMapper mapper = new ObjectMapper();

                        ArrayList<ShiftModel> shiftData = new ArrayList<>();
                        shiftData = mapper.readValue(shift.toString(), new TypeReference<ArrayList<ShiftModel>>() {});

                        ArrayList<ShiftPhotoModel> shiftPhotoData = new ArrayList<>();
                        shiftPhotoData = mapper.readValue(shiftPhoto.toString(), new TypeReference<ArrayList<ShiftPhotoModel>>() {}); 
                        .
                        .
                        ...

衷心希望您对此问题的帮助或解决方案,非常感谢您的 cmets 或一些建议,谢谢!

【问题讨论】:

标签: java android arrays json android-studio


【解决方案1】:

检查这个-首先将base64转换为位图并尝试将其放置在imageview中

  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
  bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
  byte[] imageBytes = byteArrayOutputStream.toByteArray();
  String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
  imageBytes = Base64.decode(imageString, Base64.DEFAULT);
  Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
  imageView.setImageBitmap(decodedImage);

【讨论】:

  • 首先感谢您对这个问题的回答,但主要问题是我无法配置如何解决 JSONObject 转换为 string() 异常问题
  • 我认为这是因为它实际上导致了巨大而沉重的字符串stackoverflow.com/questions/64752148/…检查这个
  • 我明白了,我得到的 JSON 响应似乎很重,导致程序变慢,但如果我忽略警告,它实际上工作得很好,谢谢
猜你喜欢
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2021-07-15
相关资源
最近更新 更多