使用 Http 服务时处理异常
在我的应用程序中,我试图处理因使用 http Web 服务而可能出现的异常
,据我所知,到目前为止,可能会出现 3 种类型的异常
传输异常
协议异常
超时异常
我在我的android应用程序中使用HttpClient
类, 我不必提供登录凭据作为我的 http 请求的一部分,因此我认为协议异常很少发生,我知道这不是发生协议异常的唯一原因,但它是
传输中 最常见的原因我读到 HttpClient
是一个强大的处理传输异常的方法,因此它缺陷传输异常并重试请求,
因此现在对于超时异常(连接超时和套接字超时)重试请求是正确的处理这些异常的方法?或者有更强大的解决方案?
In my application i am trying to handles Exceptions
that can arise from consuming http web services , as i know so far there are 3 types of exceptions that can arise
Transport Exception
Protocol Exception
Timeout Exception
i am using HttpClient
class in my android application ,
i don't have to supply login credential as a part of my http request so i think the Protocol Exception is rarely to occur , i know its not the only reason that a protocol exception can occur but its the most commonly one ,
for the transport exception i read that HttpClient
is a robust to handle transport exception so it defects transport exception and retrying requests ,
so now for the timeout exception ( connections time out and socket time out ) is retrying the request is the proper way to handle these exception ? or there is more robust solution ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您未在 http 请求中设置关联属性,则不应收到任何超时异常。默认情况下它们设置为 0,这意味着没有超时。
但是,按照您的建议给予适当的超时并处理异常似乎是正确的方法。
也许您还应该考虑添加一些代码来检测网络状态。
You should not get any timeoutexception if you don't set the associated properties in your http request. They are set to 0 by default and that means there is no timeout.
But giving an appropriate timeout and handling the exception as you propose to do it seems to be the right way.
Maybe you should also consider adding some code to detect network state.