介绍
这篇文章主要介绍如何解决Java中NegativeArraySizeException异常的问题,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
问题描述:服务器接收后台返回的报文时,提示java.lang.NegativeArraySizeException
分析:这种异常返回的原因,一般情况下没有报文提示为返回空报文,初步分析是响应报文流长度出了问题
百度一下类似的情况:https://stackoverflow.com/questions/11207897/negative-array-size-exception
节选部分内容:
{试 ,,,,connection =, (HttpConnection) Connector.open (“http://someurl.xml" Connector.READ_WRITE); ,,,,URLEncodedPostData postData =, new URLEncodedPostData (URLEncodedPostData.DEFAULT_CHARSET,,假); ,,,,postData.append (“username",,“loginapi"); ,,,,postData.append (“password",,“myapilogin"); ,,,,postData.append (“term",,字); , ,,,,connection.setRequestMethod (HttpConnection.POST); ,,,,connection.setRequestProperty (“Content-Type",“应用程序/x-www-form-urlencoded"); ,,,,connection.setRequestProperty (“User-Agent",“配置文件/midp - 2.0,配置/cldc - 1.0“); ,,,,requestOut =, connection.openOutputStream (); ,,,,requestOut.write (postData.getBytes ()); ,,,,String contentType =, connection.getHeaderField (“Content-type"); ,,,,detailIn =, connection.openInputStream (),,,,,,,,, ,,,,int length =, (int), connection.getLength (); ,,,,ByteArrayOutputStream baos =, new ByteArrayOutputStream (); ,,,,如果(length 祝辞,0){//这里长度没有判定的情况下,byte 数组长度若为1会产生错误 ,,,,,,,,byte 信息[],=,new 字节(长度); ,,,,,,,,int bytesRead =, detailIn.read(信息); ,,,,,,,,,(bytesRead 祝辞,0),{ ,,,,,,,,,,,,baos.write(信息,,0,,bytesRead); ,,,,,,,,,,,,bytesRead =, detailIn.read(信息); ,,,,,,,,,,,,} ,,,,,,,,baos.close (); ,,,,,,,,connection.close (); ,,,,,,,,requestSuceeded (baos.toByteArray (),, contentType); , ,,,,,,,,detailIn.read(信息); ,,,,} ,,,, ,,,,{ ,,,,,,,,,System.out.println (“Negative array size"); ,,,,} ,,,,,,,,,,requestOut.close (); ,,,,,,,,,,detailIn.close (); ,,,,,,,,,,connection.close (); ,,,}
结论:HTTP服务器在返回响应报文的时候,没有进行内容。长度长度判断,按照常规流程响应了错误长度的报文,导致了接收方报文长度异常
以上是“如何解决Java中NegativeArraySizeException异常的问题”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!