在 Activity 建立 HTTP 连接之前,Android 服务无法建立 HTTP 连接

发布于 2024-08-31 17:24:36 字数 2014 浏览 4 评论 0原文

这是我的帖子:

我试图让自己尽可能清楚:

  1. 应用程序中的类的描述
  2. 代码架构的描述(按服务和活动)
  3. 应该发生的情况的描述
  4. 实际发生情况的描述
  5. Logcat
  6. Source code

应用程序类的描述

我的应用程序中的

  • :启动时启动的服务,由 MAJService 类管理
  • ConsoNRJ 类管理的活动
  • 工作人员类它将通过 HTTP 获取 HTML 页面,名为 MAJDonnees
  • 一个名为 InfosConso 的帮助器类,它将实例化一个 MAJDonnees 工作器。

处理描述

当 Service 启动时,它将:

  • 创建一个 InfosConso 对象
  • InfosConso 对象将创建一个 MAJDonnees
  • MAJDonnees< /code> 对象将使用 HttpURLConnection 执行 HTTP 查询

该活动的作用几乎相同:

  • 创建一个 InfosConso 对象
  • InfosConso 对象将创建一个 >MAJDonnees
  • 这个 MAJDonnees 对象将创建一个 AsyncTask,然后使用 HttpURLConnection 执行 HTTP 查询。

您可以看到一个图表,可能会帮助您理解我的代码:
code.google.com/p/consonrj/wiki/CodeStructure


应该发生什么

由于 Activity 和 Service 基本上运行相同代码(在 MAJDonnees 类中),因此它们应该行为方式完全相同:获取 HTML 页面并解析它们

实际发生的情况

从服务运行时,HttpURLConnectionsMAJDonnees 中处理时返回错误结果。 名为 hHttpURLConnections 实例给出无效的 HTTP 响应:h.getResponseCode() 返回 -1。
我不知道如何获取更多调试信息。

但是,如果活动已启动,则 HTTP 连接会在 MAJDonnees 中处理时工作,实际上 h.getResponseCode() 返回 200 (HTTP 200 OK)。

那么如果服务再次运行(计划每 x 分钟运行一次),HTTP 连接就会正常工作!


Logcat

您可以首先运行服务的 logcat,然后运行活动,然后再次运行服务,然后查看行为。
http://pastebin.com/DGc8fym2
对不起,这是法语,我希望你还能猜到! :)

源代码

如果需要,您可以在以下位置查看整个源代码:
code.google.com/p/consonrj/source/browse/#svn/trunk


我希望你能帮我调试这个!我的服务需要在设备启动时以及每 x 小时/天从 Internet 获取数据,并且不需要首先启动该活动!

谢谢。

Here is my post:

I'm trying to make myself as clear as possible:

  1. Description of the classes in my application
  2. Description of the code architecture (by the Service and by the Activity)
  3. Description of what should happen
  4. Description of what actually happens
  5. Logcat
  6. Source code

Description of the application classes

I have in my application:

  • a service that launches on boot, managed by the MAJService class
  • an activity that is managed by the ConsoNRJ class
  • a worker class that will get HTML pages over HTTP, called MAJDonnees
  • an helper class that is called InfosConso and that will instantiate a MAJDonnees worker.

Description of the processing

When the Service starts, it will:

  • create an InfosConso object
  • this InfosConso object will create a MAJDonnees
  • this MAJDonnees object will do HTTP queries using HttpURLConnection

The activity does almost the same:

  • create an InfosConso object
  • this InfosConso object will create a MAJDonnees
  • this MAJDonnees object will create an AsyncTask, and then do the HTTP queries using HttpURLConnection

You can see a graph that might help you understand my code:
code.google.com/p/consonrj/wiki/CodeStructure


What should happen

Since the Activity and the Service basically run the same code (in the MAJDonnees class), they should behave exactly the same way: fetch HTML pages and parse them

What actually happens

When ran from the service, the HttpURLConnections returns a bad result while processing in MAJDonnees.
The HttpURLConnections instance called h gives an invalid HTTP response: h.getResponseCode() returns -1.
I don't know how to get more debug information.

However, if the activity is started, the HTTP connections work while processing in MAJDonnees, indeede h.getResponseCode() returns 200 (HTTP 200 OK).

THEN if the Service runs again (it is scheduled to run every x minutes), the HTTP connections work!


Logcat

You can have a logcat of the service first ran, then the activity, then again the service, and see the behavior.
http://pastebin.com/DGc8fym2
I'm sorry it is French, I hope you can still guess! :)

Source code

If you want, you can have a look at the whole source at:
code.google.com/p/consonrj/source/browse/#svn/trunk


I hope you can help me debug this! My service needs to fetch data from Internet when the device starts and every x hours/days, and shouldn't need the activity to start first!

Thanks.

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

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

发布评论

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

评论(1

迷你仙 2024-09-07 17:24:36

非常感谢布拉德的建议。

事实上,我自己已经解决了这个问题,通过在应用程序中生成更多的调试详细信息。

实际上,该活动使用 Compte 类从手机内存中的文件加载用户凭据,而服务则没有。因此该服务没有登录 HTML 页面的用户凭据。因此,服务发送带有空凭据的登录请求,直到活动加载它们。

现在该服务还使用 Compte 类加载凭据,它运行良好。
我很抱歉我没有更彻底地调查我的应用程序。

不管怎样,谢谢布拉德!非常感谢。

Thank you very much for your advice Brad.

In fact I have solved the problem myself, by generating more debug verbose in the application.

Actually the activity uses the Compte class that loads the user credentials from files in the phone memory, and the service didn't. So the service didn't have the user credentials to log onto the HTML page. So the service sent login request with empty credentials, until the activity loaded them.

Now that the service also loads the credentials using the Compte class, it works well.
I am very sorry I didn't investigate more thoroughly my app.

Thanks anyway Brad! Much appreciated.

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