3
When the JSON string is too large, there is loss of part of the string in the upload. I am sending like this:
private void btComunicarActionPerformed(java.awt.event.ActionEvent evt) {
List<PessoaMOD> pessoas = new ArrayList<PessoaMOD>();
for (int i = 1; i <= 2000; i++) {
pessoas.add(new PessoaMOD(i, "Pessoa " + i));
}
try {
Socket cliente = new Socket("127.0.0.1", 12345);
enviarMensagem(codificarListarDiretorio(pessoas), cliente);
} catch (Exception e) {
System.out.println("Erro: " + e.getMessage());
} finally {
}
}
public ByteArrayOutputStream codificarListarDiretorio(List<PessoaMOD> pessoas) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
Gson gson = new GsonBuilder().create();
dos.write(gson.toJson(pessoas).getBytes());
return bos;
}
public void enviarMensagem(ByteArrayOutputStream mensagem, Socket socket) throws IOException {
byte[] msg = mensagem.toByteArray();
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeInt(msg.length); //O tamanho da mensagem
out.write(msg); //Os dados
out.flush();
}
and get on the server like this:
int tamanhoMsg = entrada.readInt();
byte[] bufferJson = new byte[tamanhoMsg];
entrada.read(bufferJson, 0, bufferJson.length);
String json = new String(bufferJson);
Only the Json String doesn’t come complete when it’s too big.
What happens is that the number of bytes is greater than the lenght support, so the message size does not send full.
I also tried sending by the method writeUTF();
But as the String is large, it generates this error: encoded string Too long: 677789 bytes
how is instantiated out ?
– Thiago Friedman
not "out", is "dos", had put wrong. You would have a light to give me in this problem? rsrs
– Rodrigo Lima