【发布时间】: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