litesuits / android-lite-http

LiteHttp is a simple, intelligent and flexible HTTP framework for Android. With LiteHttp you can make HTTP request with only one line of code! It could convert a java model to the parameter and rander the response JSON as a java model intelligently.
http://litesuits.com?form=ghttp
Apache License 2.0
830 stars 347 forks source link

取消网络请求时,数据还在继续下载 #21

Open mumu7818 opened 8 years ago

mumu7818 commented 8 years ago

在调用了request.cancel()后,表面上看起来像是取消下载了,但实质服务器还能给客户端发数据。 分析了原因,是因为DataParser中stream.close()不能立即关闭,需要接收完全部数据,inputstream才能关闭导致。这说明数据链路还是连接的,这样取消下载就没什么意义,还是会消耗流量,但我不知道怎么修改,有大牛知道怎么修改吗? public T readFromNetStream(InputStream stream, long len, String charSet) throws IOException { if (stream != null) { try { this.data = parseNetStream(stream, len, charSet); } finally { stream.close(); } } return this.data; }

litesuits commented 8 years ago

这样看你数据量的大小; 如果你数据小于 框架buffer size 4096 那么无法停止,如果你数据大于4096那么会在完成一波buffer后及时停止。代码如下: while (!request.isCancelledOrInterrupted() && (l = is.read(buff)) > 0) { swapStream.write(buff, 0, l); readLength += l; }