-2

I'm getting a response from an URL which I'm storing to a string. The Response is in JSON format and it's about 1800 lines. When I use a String to read the data, around 800 lines it comes like this..

{"carrierFsCode":"UA","flightNumber":"935","se...

Initially I though String might not be able to handle it and I tried to use StringBuffer setting a minimumcapacity and all but I'm getting the same problem. This is the part where I'm having the problem.

   String str = "";
   while (null != (str = br.readLine())) {
        temp = str;
   }

Any suggestions why this is happening.

PS: The URL is fine. When I try to store it to a file I get the entire JSON.

Vinnie
  • 452
  • 5
  • 24
  • By storing in string what do you want ? show on console or some other operation ? – user3145373 ツ Sep 30 '14 at 06:57
  • What are you trying to get it to read the string as? – christopher Sep 30 '14 at 06:58
  • 3
    No, there is no size for `String`. You are probably just viewing it in a manner that displays it shortened. – Keppil Sep 30 '14 at 06:58
  • Maybe the best answer of [this similar question](http://stackoverflow.com/questions/816142/strings-maximum-length-in-java-calling-length-method) will help you. – ekostadinov Sep 30 '14 at 06:58
  • @user3145373ツ I'm using to get data out, but when it was not happeing, I was debugging and reached to this point. – Vinnie Sep 30 '14 at 07:01
  • @Keppil Even I thought the same, but now i'm confused. – Vinnie Sep 30 '14 at 07:01
  • The only limit the string has is the memory. If you have not enaugh RAM for your JVM, you won't be able to store all the content in the string – BackSlash Sep 30 '14 at 07:01
  • 1
    It sounds like your debugger tool is shortening the string for the purpose of printing. Have you tried using `System.out.println`? This should output the entire string to your console. – Jason Sep 30 '14 at 07:02
  • @ekostadinov Thanks bro, I did see that post as well – Vinnie Sep 30 '14 at 07:02
  • converting from JSON to String should not be a problem irrespective of the length of the JSON. may be there is some other issue did you investigate and what error you get – vikeng21 Sep 30 '14 at 07:02
  • @Vineeth your requirement is to get whatever String comes in json that meean u need full String whatever it is? is it? – santhosh Sep 30 '14 at 07:02
  • @Vineeth : are you displaying it on console ? – user3145373 ツ Sep 30 '14 at 07:03
  • what makes you think you are not recieving all the data? how are you appending the response? You will always have the last part of the json, if it has line breaks, with your current implementation. – BatScream Sep 30 '14 at 07:03
  • refer this [link]http://stackoverflow.com/questions/816142/strings-maximum-length-in-java-calling-length-method – santhosh Sep 30 '14 at 07:07
  • Alright guys... Palm Face... I @Jason said I did use that same string and try to save it to a file. I worked. The Debugger was not showing the entire JSON... Sorry for the dumb question...but thanks a lot for quick help guys.. – Vinnie Sep 30 '14 at 07:07

2 Answers2

4

I guess that the trailing dots is what you see when watch your result using debugger. This is not the String limitation, this is the debugger feature.

If you want to see really long string in debugger use expressions view and type there System.out.println(mystring) ( where mystring is the name of your string variable). Then see the results on console.

BUT.

It seems that you are probably on a wrong way. String stores data in array that is limited by Integer.MAX_VALUE. This number is pretty high, but in some case you can reach the limit. Moreover this way is highly ineffective in terms of performance and memory consumption. Typically you read JSON in order to parse it and turn it into object. So, do it using stream: send input stream to the JSON parser. The parsing will be done while reading from the stream, the whole JSON hopefully will not stored as one huge chunk in memory.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

The following snippet will append all lines into a StringBuilder which you can then call .toString() to get the data.

StringBuilder sb = new StringBuilder();   
String str = "";
while (null != (str = br.readLine())) {
    sb.append(str);
}
Jason
  • 11,744
  • 3
  • 42
  • 46