BOOT_COMPLETED 中的 HTTP 请求期间出现 IOException

发布于 2024-12-11 04:42:57 字数 1557 浏览 2 评论 0原文

我有一个 Android 应用程序,其中包含将数据发布到服务器的语句。

HttpResponse httpresponse = httpclient.execute(httppost);

当我启动应用程序时,这条语句执行得很好。我已使我的应用程序在启动后自动启动。当它启动时,我在这一行收到一个 IOEXCeption 消息,其中包含我的主机名消息。主机一直在运行,因为当我手动启动应用程序时,相同的语句工作正常。仅当手机启动时才会出现此问题。

以前有人遇到过这个问题吗?


康曼斯软件, 我在 onReceive 调用中有这个新方法来检查网络连接。这样我的问题就解决了,这意味着它发布了“启动完成”消息并继续成功执行http请求,但看起来它没有退出,同时看起来“不在线”消息不断出现。怎么会发生呢?这不是正确的解决方案吗?

public void onReceive(Context context, Intent intent) 
        {
            while(!isOnline(context) )
            {
                Toast toast = Toast.makeText(context, "Not online", Toast.LENGTH_SHORT);
                toast.show();   
            }

                Toast toast = Toast.makeText(context, "in boot completed", Toast.LENGTH_SHORT);
                toast.show();

            }
        }

        protected boolean isOnline(Context context) {

            ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);    
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnected())
            {
                Toast toast = Toast.makeText(context, "returned true", Toast.LENGTH_SHORT);
                toast.show();
                return true;        
            } 
            else 
            {
                Toast toast = Toast.makeText(context, "returned false", Toast.LENGTH_SHORT);
                toast.show();
                return false;
            }
        }

I have an Android application that has this statement that posts data to the server.

HttpResponse httpresponse = httpclient.execute(httppost);

When I start the application, this statement executes fine. I have made my application automatially start after booting. When it boots up, I am getting an IOEXCeption at this line with message my hostname. The host is running all the time as the same statement works fine when I start the application manually. This issue occurs only when the phone boots up.

Has anyone encountered this issue before?


CommansWare,
I have this new method in my onReceive call to check the network connection. With this my problem is resolved, meaning it posts the "in boot Completed" message and goes on executing the http request successfully but it looks like it does not exit the while look as the "not online" message showing up endlessly. How can it happen? Is this not the correct solution?

public void onReceive(Context context, Intent intent) 
        {
            while(!isOnline(context) )
            {
                Toast toast = Toast.makeText(context, "Not online", Toast.LENGTH_SHORT);
                toast.show();   
            }

                Toast toast = Toast.makeText(context, "in boot completed", Toast.LENGTH_SHORT);
                toast.show();

            }
        }

        protected boolean isOnline(Context context) {

            ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);    
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnected())
            {
                Toast toast = Toast.makeText(context, "returned true", Toast.LENGTH_SHORT);
                toast.show();
                return true;        
            } 
            else 
            {
                Toast toast = Toast.makeText(context, "returned false", Toast.LENGTH_SHORT);
                toast.show();
                return false;
            }
        }

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

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

发布评论

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

评论(1

爱格式化 2024-12-18 04:42:57

此时可能没有有效的互联网连接。毕竟,设备刚刚从关闭状态开启。这与任何其他操作系统没有什么不同。

您可能需要采取一些措施来延迟 HTTP 操作,直到 Internet 连接可用为止。例如,您的 BOOT_COMPLETED 接收器可以使用 AlarmManagerset() 安排在几分钟内再次获得控制权,届时设备希望会更加稳定。当然,您还需要使用 ConnectivityManager 来确定当时是否也有 Internet 连接,并且您可能更愿意执行一些复杂的接收器工作来侦听 ConnectivityManager< /code> 广播,也许可以比使用 AlarmManager 路由更快地完成 HTTP 工作。

There may not be an active Internet connection at that point in time. After all, the device just powered on from being turned off. This is no different than any other OS.

You may need to do something to delay your HTTP operations until such time as an Internet connection is available. For example, your BOOT_COMPLETED receiver could use AlarmManager and set() to arrange to get control again in a few minutes, by which time the device will hopefully be more stable. Of course, you will also want to use ConnectivityManager to determine whether there is an Internet connection at that time as well, and you may prefer to do some sophisticated receiver work to listen for a ConnectivityManager broadcast, to perhaps do your HTTP work a bit more quickly than you would get with the AlarmManager route.

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