使用按长度自动接收数据的方式时,当client 端异常时,receivethread 还一直循环接收,反复调用onResponse() 显示接收为null,造成APP 卡顿。解觉办法:
在SocketInputReader中的readToLength()函数捕获异常并抛出异常,这时receivethread 会结束socket链接并退出。
do { readCount = this.inputStream.read(buffer, index, length - index); index += readCount; } while (readCount != -1 && index < length);
该段代码read函数抛出异常后,异常处理返回null,修改为抛出异常即可。