1

I am trying to get JSONObject from following snippet of code.

But since response body is large it can not store results completely.

Final aim is to convert it to jsonobject or store as Json File

CloseableHttpResponse apiResponse;
apiResponse = getReorting(authToken);


String responseString = EntityUtils.toString(apiResponse.getEntity(), "UTF-8");
JSONObject responseJson = new JSONObject(responseString);
Vishal
  • 35
  • 5
  • what error are you getting to say that it's to large for String to store? Because from my knowledge String can store up to 2,147,483,647 characters – Morph21 Dec 14 '21 at 11:55
  • There is no error but responseString got truncated. – Vishal Dec 14 '21 at 11:57
  • count returned characters, if it's less than 2,147,483,647 then increase your heap size times 2, count returned characters if number it's the same as in first count then your error is not about String at all – Morph21 Dec 14 '21 at 11:59
  • Thanks, length of line shows 43,800 (char count without line ending 43,800). But it is not throwing error. while debugging i see line truncated with ....... "iso": "2021-12-10T01:52:25-08:00", "timestamp": ... – Vishal Dec 14 '21 at 12:05
  • welll... that text sounds more like truncated by ide for viewing. Try to print it to console or to file, then check if it's still truncated – Morph21 Dec 14 '21 at 12:07
  • Hi I tried to create file using string response PrintWriter outFile = new PrintWriter("src/test/resources/response.json"); outFile.println(responseString); but only 32,768 characters were written – Vishal Dec 14 '21 at 12:59
  • check what EntityUtils.toString does in code – Morph21 Dec 14 '21 at 13:06
  • EntityUtils just returns HTTP Response Content-Length: 46906 returned from HTTP Response Is it a reason that sting is getting truncated? – Vishal Dec 14 '21 at 13:22
  • I must disagree with you. Look here: https://stackoverflow.com/questions/30642237/what-exactly-returns-entityutils-tostringresponse?noredirect=1&lq=1 – Morph21 Dec 14 '21 at 13:26
  • thanks I will check – Vishal Dec 14 '21 at 13:38
  • Tried lot of combinations for entity response but no success. when converting to string response is getting trucated. Could you please suggest? – Vishal Dec 14 '21 at 15:11
  • can you provide code to replicate that request? – Morph21 Dec 15 '21 at 07:33

0 Answers0