Right now I'm able to send JSON strings to my Android app over the network fine. The only thing is that the messages are no larger than a few hundred characters long at the moment. This will soon expand to thousands of characters per JSON string. How can I ensure that I've received the entire JSON string before sending it to the parser (which would otherwise crash)?
For example, a basic way of how I'm receiving data in a Thread:
while (true)
{
try
{
// Read from the DataInputStream
bytes = mmDataIn.read(buffer); //buffer is a 4096 size byte[]
Log.d("JSON", "dataread: " + String.valueOf(bytes)); //only prints stuff > 0
parseJSON(buffer); //will not work unless full JSON
}
catch (IOException e)
{
Log.e("JSON", "disconnected", e);
break;
}
}
I was planning on using a StringBuilder to piece together the JSON string, but I don't know how to tell when it is done.
I was reading this: How to identify end of InputStream in java
But what would be the best way to do it for a JSON string? I'm assuming this is very common and there's a standard way of handling it. Thanks.