Android 通过httpclient 调用碰到的问题总结

本文总结了在Android中使用HttpClient进行HTTP请求时遇到的两个主要问题:1. 内容被消耗导致的IllegalStateException;2. 缺失Content-length头导致的错误。解决方案包括确保只调用一次httpEntity.getContent(),以及处理无Content-length头的情况,了解http chunked编码,并使用工具如Firebug或HttpWatch进行调试。
摘要由CSDN通过智能技术生成

问题一:

1. java.lang.IllegalStateExceptio n: Content has been consumed

这个问题是多次调用httpEntity.getContent()导致的, entity中的内容只能读取一次, 参考如下:

You can retrieve the content from the entity only once. If you have 
already extracted the content somewhere, and you try to fetch it 
again, it will throw this IllegalStateException. Check you code and 
make sure that you make this call only once. 


2.  http header 中的content-length

这个问题导致了各种千奇百怪的error,反正就无法返回数据。 

比如; source not found,   Runtime Exception等等问题,其实问题出现在测试服务器上的Nignx的返回头部没有包含Content-length这项信息,所用通过代码

int length = ( int ) httpEntity.getContentLength()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值