简体   繁体   中英

Android Studio - Java (JSONObject/JSONArray) Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate org.json.JSONObject.toString()

Kindly asking for help, I am trying to get a JSON response from an API and the response was not null and kinda long because it has a Base64 encoded picture data This is the fragment from the response

and this is the part of the code i use to get the JSON response and convert it to 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>>() {}); 
                        .
                        .
                        ...

I kindly hope for your help or solution for this problem, and i really appreciate for comments or some suggestion, thanks!

Check this- First convert the base64 to bitmap and try to place it in 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM