I'm trying to make myself as clear as possible:
I have in my application:
MAJService
class ConsoNRJ
class MAJDonnees
InfosConso
and that will instantiate a MAJDonnees
worker. InfosConso
object InfosConso
object will create a MAJDonnees
MAJDonnees
object will do HTTP queries using HttpURLConnection
InfosConso
object InfosConso
object will create a MAJDonnees
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
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
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!
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! :)
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.