【问题标题】:IOException during HTTP request in BOOT_COMPLETEDBOOT_COMPLETED 中 HTTP 请求期间的 IOException
【发布时间】:2012-03-02 15:57:43
【问题描述】:

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

HttpResponse httpresponse = httpclient.execute(httppost);

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

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


CommsWare, 我在 onReceive 调用中有这个新方法来检查网络连接。这样我的问题就解决了,这意味着它会发布“in boot Completed”消息并继续成功执行 http 请求,但看起来它没有退出 while 看起来“not online”消息无休止地出现。怎么会发生?这不是正确的解决方案吗?

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;
            }
        }

【问题讨论】:

    标签: android httpclient boot


    【解决方案1】:

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

    您可能需要做一些事情来延迟您的 HTTP 操作,直到 Internet 连接可用为止。例如,您的BOOT_COMPLETED 接收器可以使用AlarmManagerset() 安排在几分钟后再次获得控制权,届时设备有望更加稳定。当然,您还需要使用ConnectivityManager 来确定当时是否有 Internet 连接,并且您可能更喜欢做一些复杂的接收器工作来监听ConnectivityManager 广播,也许做您的 HTTP比使用AlarmManager 路由更快。

    【讨论】:

      猜你喜欢
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      • 2015-02-16
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 2012-10-19
      相关资源
      最近更新 更多