String is being truncated when its too long

后端 未结 5 1616
执笔经年
执笔经年 2020-12-31 18:19

I am trying to get a JSON response from our server and the response string seems is always being truncated when the string length reaches to around 5525 characters.

5条回答
  •  情歌与酒
    2020-12-31 18:57

    String line = reader.readLine();

    I believe the above line in your program is the problem. It reads a line from the server response and the String line. If the server response has a line feed (\n) then the reader will not be able to read the line after that.

    use the following to avoid that..

    if (responseCode == HttpURLConnection.HTTP_OK) {

                InputStream in = ((URLConnection) httpConnection).getInputStream();
                int len = 0;
    
    
                byte[] data1 = new byte[1024];
                while ( -1 != (len = in.read(data1)) )
                    dataFromServer.append(new String(data1, 0, len));
    
            }
    

提交回复
热议问题