Java 无法从inputstream读取大型JSON数据
你好, 我正在使用Java 无法从inputstream读取大型JSON数据,java,httpurlconnection,Java,Httpurlconnection,你好, 我正在使用httpUrlConnection从Web服务检索json字符串。然后我从连接中获取inputStream Java(TM) SE Runtime Environment (build 1.7.0_45-b18) 然后,我使用以下函数读取inputstream以获取JSON jsonString = readJSONInputStream(mHttpUrlconnection.getInputStream()); 但是,如果json很小,比如说600字节,那么一切都正常(因
httpUrlConnection
从Web服务检索json字符串。然后我从连接中获取inputStream
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
然后,我使用以下函数读取inputstream以获取JSON
jsonString = readJSONInputStream(mHttpUrlconnection.getInputStream());
但是,如果json很小,比如说600字节
,那么一切都正常(因此上面的代码适用于较小的数据)。但是我有一些JSON的大小大约是15000字节
,所以我将最大大小设置为16092
但是,它只读取了关于6511
的JSON文件,只是将其截断
我不明白,如果JSON很小,就没有问题。但对于更大的JSON,每次都会以相同的大小进行剪切
我做错什么了吗。有什么我要查的吗
非常感谢您的建议,请尝试以下代码:
private String readJSONInputStream(final InputStream inputStream) {
log.log(Level.INFO, "readJSONInputStream()");
Reader reader = null;
try {
final int SIZE = 16092;
char[] buffer = new char[SIZE];
int bytesRead = 0;
int read = 0;
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), SIZE);
bytesRead = reader.read(buffer);
String jsonString = new String(buffer, 0, bytesRead);
log.log(Level.INFO, "bytesRead: " + bytesRead + " [ " + jsonString + " ]");
/* Success */
return jsonString;
}
catch(IndexOutOfBoundsException ex) {
log.log(Level.SEVERE, "UnsupportedEncodingexception: " + ex.getMessage());
}
catch(IOException ex) {
log.log(Level.SEVERE, "IOException: " + ex.getMessage());
}
finally {
/* close resources */
try {
reader.close();
inputStream.close();
}
catch(IOException ex) {
log.log(Level.SEVERE, "IOException: " + ex.getMessage());
}
}
return null;
}
请尝试以下代码:
private String readJSONInputStream(final InputStream inputStream) {
log.log(Level.INFO, "readJSONInputStream()");
Reader reader = null;
try {
final int SIZE = 16092;
char[] buffer = new char[SIZE];
int bytesRead = 0;
int read = 0;
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), SIZE);
bytesRead = reader.read(buffer);
String jsonString = new String(buffer, 0, bytesRead);
log.log(Level.INFO, "bytesRead: " + bytesRead + " [ " + jsonString + " ]");
/* Success */
return jsonString;
}
catch(IndexOutOfBoundsException ex) {
log.log(Level.SEVERE, "UnsupportedEncodingexception: " + ex.getMessage());
}
catch(IOException ex) {
log.log(Level.SEVERE, "IOException: " + ex.getMessage());
}
finally {
/* close resources */
try {
reader.close();
inputStream.close();
}
catch(IOException ex) {
log.log(Level.SEVERE, "IOException: " + ex.getMessage());
}
}
return null;
}
您的代码很可能在此处被破坏:
String readResponse(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
你应该循环阅读。我认为您并没有读取流中的所有字符。很可能您的代码在此处被破坏:
String readResponse(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
你应该循环阅读。我认为您并没有从流中读取所有字符。您应该继续从缓冲区读取,直到bytesRead=-1 我怀疑读卡器正在缓冲数据,并且一次只返回一定数量的数据
尝试循环直到bytesRead==-1,每次循环都会将读取的数据附加到结果字符串中。您应该继续从缓冲区读取数据,直到bytesRead=-1为止 我怀疑读卡器正在缓冲数据,并且一次只返回一定数量的数据
尝试循环直到bytesRead==-1,每次循环都会将读取的数据附加到结果字符串中。HTTP GET请求没有大小限制吗?我正在使用POST not GET,即mhttpullconnection.setRequestMethod(“POST”);HTTP GET请求没有大小限制吗?我正在使用POST not GET,即mhttpullconnection.setRequestMethod(“POST”);现在测试你的代码。您不应该在while循环中测试-1而不是null吗?它工作得非常好,我已经多次使用相同的代码。这会给您带来麻烦吗?返回值现在仍然被截断/截断,以便对代码进行计量。您不应该在while循环中测试-1而不是null吗?它工作得非常好,我已经多次使用相同的代码。这会给你带来麻烦吗?我的报税表还是被截断了