Java TCP服务器客户端大型json
我目前连接了TCP服务器(java桌面应用程序)和客户端(android应用程序) 我可以将json字符串从客户端发送到服务器,从服务器发送到客户端,我的问题是有时我需要从服务器发送一个大字符串,这个字符串会被剪切 这是我的代码: 服务器端Java TCP服务器客户端大型json,java,android,json,tcp,Java,Android,Json,Tcp,我目前连接了TCP服务器(java桌面应用程序)和客户端(android应用程序) 我可以将json字符串从客户端发送到服务器,从服务器发送到客户端,我的问题是有时我需要从服务器发送一个大字符串,这个字符串会被剪切 这是我的代码: 服务器端 public void sendResponse(String response){ PrintWriter writer = new PrintWriter(BufferedWriter( new Outp
public void sendResponse(String response){
PrintWriter writer = new PrintWriter(BufferedWriter(
new OutputStreamWriter(mClientSocket.getOutputStream())), true);
if(writer != null && !writer.checkError()){
writer.println(response);
writer.flush();
}
}
客户端
BufferedReader input = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
String response;
while((response = input.readLine()) != null && mRun){
Data data = new Gson().fromJson(response, Data.class);
//when i try to convert the response to Data object using Gson
//it gives me a com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated array at line 1 column 106498
}
正如您所看到的,客户端发生错误是因为字符串太大,所以被切断,
有人能告诉我在这种情况下该怎么办吗
提前谢谢。我建议你用a,而不是试着读台词。大概
public void sendResponse(String response) {
OutputStream os = mClientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
dos.writeUTF(response);
dos.flush();
}
然后,在客户机上
InputStream is = mSocket.getInputStream();
DataInputStream dis = new DataInputStream(is);
String response = dis.readUTF(); // <-- not just one line. the entire string.
Data data = new Gson().fromJson(response, Data.class);
及
编辑
public void sendResponse(String response) {
OutputStream os = mClientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(new GZIPOutputStream(os));
byte[] buff = response.getBytes("UTF-8");
dos.writeInt(buff.length);
dos.write(buff);
dos.flush();
}
然后阅读
InputStream is = mSocket.getInputStream();
DataInputStream dis = new DataInputStream(new GZIPInputStream(is));
int len = dis.readInt();
byte[] buff = new byte[len];
dis.readFully(buff);
String response = new String(buff, "UTF-8");
Data data = new Gson().fromJson(response, Data.class);
我建议你用一个字母来代替读台词。大概
public void sendResponse(String response) {
OutputStream os = mClientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
dos.writeUTF(response);
dos.flush();
}
然后,在客户机上
InputStream is = mSocket.getInputStream();
DataInputStream dis = new DataInputStream(is);
String response = dis.readUTF(); // <-- not just one line. the entire string.
Data data = new Gson().fromJson(response, Data.class);
及
编辑
public void sendResponse(String response) {
OutputStream os = mClientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(new GZIPOutputStream(os));
byte[] buff = response.getBytes("UTF-8");
dos.writeInt(buff.length);
dos.write(buff);
dos.flush();
}
然后阅读
InputStream is = mSocket.getInputStream();
DataInputStream dis = new DataInputStream(new GZIPInputStream(is));
int len = dis.readInt();
byte[] buff = new byte[len];
dis.readFully(buff);
String response = new String(buff, "UTF-8");
Data data = new Gson().fromJson(response, Data.class);
谢谢Elliot,我尝试了这两种方法,它给了我java.io.UTFDataFormatException:编码字符串太长:服务器上有331533字节side@anonimoo90然后,您需要将
字符串
拆分为一段,或者将其作为字节[]
发送。我应该如何将其拆分为几段?如何接收呢?这次我可以从服务器发送数据了。但在客户端,当我检索它时,我得到一个IOException,它是由以下原因引起的:java.util.zip.DataFormatException:invalid blocktype@anonimoo90如果在客户端使用GZIPInputStream,则必须在服务器上使用GZIPOutputStream。谢谢Elliot,我尝试了这两种方法,但它给了我java.io.UTFDataFormatException:编码字符串太长:服务器上有331533字节side@anonimoo90然后,您需要将字符串
拆分为一段,或者将其作为字节[]
发送。我应该如何将其拆分为几段?如何接收呢?这次我可以从服务器发送数据了。但在客户端,当我检索它时,我得到一个IOException,它是由以下原因引起的:java.util.zip.DataFormatException:invalid blocktype@anonimoo90如果在客户端上使用GZIPInputStream,则必须在服务器上使用GZIPOutputStream。