Android HttpClient异常控制问题(管理器关闭)

发布于 2025-01-02 20:03:01 字数 1506 浏览 1 评论 0原文

我的应用程序将一些字符串发送到我的服务器。
它使用HttpClient发送数据。
所有功能都很完美,没有 1 个问题..

如果我的手机处于联网状态,那么
我点击提交按钮->数据可以在我的服务器上发送。 (真的很好)

如果我的手机处于未联网(disconneing)状态(这意味着没有使用wifi或3g...)
我点击提交按钮->它显示错误对话框为“您必须检查网络”

但问题是...
一旦(大约0.5~1.5秒)我单击wifi启用按钮,然后单击提交按钮,
HttpClient执行后出现异常问题。(FC)
我可以控制异常问题吗? 我想显示错误对话框,但是,例外,我无法控制。
我认为在启用wifi(或3g)期间,执行过程中发生了问题。

这是错误语句
02-06 00:48:41.250:E / AndroidRuntime(20183):引起:java.lang.IllegalStateException:管理器已关闭。

来源

(...ready for setting params, and string datas..and so on)

            HttpPost httpPost = new HttpPost(url);
            try {
                httpPost.setEntity(mpEntity);
                httpPost.addHeader("cookie", cookie);

                //↓This execute is problem. How can I control exception?
                HttpResponse response = httpClient.execute(httpPost);

                HttpEntity resEntity = response.getEntity();
                InputStream content = resEntity.getContent();
                return content;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            httpClient.getConnectionManager().shutdown();
            return null;
        }

My app posts some strings to my server.
It use HttpClient to send datas.
All functions are perfect without 1 problem..

If My phone is on networking state then
I click submit button -> Datas can be send on my server. (real very well)

If My phone is unnetworking(disconneing) state (it means not using wifi or 3g...)
I click submit button -> It showing up Error Dialog as 'You have to check network'

But Problem is...
As soon as(about in 0.5~1.5 seconds) I click wifi enable button then I click submit button,
The HttpClient post execution showing up exception problem.(FC)
Can I control the exception problem?
I want showing up error dialog but, The exception, I can't control.
I think during wifi is enabling (or 3g), the execution something happen problems.

This is error sentence

02-06 00:48:41.250: E/AndroidRuntime(20183): Caused by: java.lang.IllegalStateException: Manager is shut down.

The Source

(...ready for setting params, and string datas..and so on)

            HttpPost httpPost = new HttpPost(url);
            try {
                httpPost.setEntity(mpEntity);
                httpPost.addHeader("cookie", cookie);

                //↓This execute is problem. How can I control exception?
                HttpResponse response = httpClient.execute(httpPost);

                HttpEntity resEntity = response.getEntity();
                InputStream content = resEntity.getContent();
                return content;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            httpClient.getConnectionManager().shutdown();
            return null;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末蓝 2025-01-09 20:03:01

I think the problematic line is

        httpClient.getConnectionManager().shutdown();

This is the original Apache documentation, without more code (and the full stack trace) is difficult to understand what's going on. I think it's just bad API usage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文