Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpClient too slow

 
Gil Carvalho
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have a little apk with external database, when i want to load an activity using HttpClient, the activity takes 6 seconds to start...i even use AsyncTask
I'm using galaxy s4

this is the code



any help here?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thing I notice is that there is a Thread.sleep(6000) statement - very suspicious if you experience a 6 second delay :-)
 
Gil Carvalho
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks for your help
I have removed that line but still the same ( 5/6 seconds to start)

Can't see what's wrong.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a doInBackground() method which does nothing but creates a Runnable which you then run on the UI thread, don't do that. You need to understand what the code you are writing does, and why it does it so that you use it correctly. The AsyncTask is used to run long tasks in the background. You do that by overriding the doInBackground() method. The runOnUiThread() method is used to execute runnables on the UI thread - so if you are on a background thread and you need to run some UI manipulation code you can do it in the correct thread. By using both of these things together you are completely negating the purpose of AsyncTask/doInBackground() - all that ends up just being a layer of useless boilerplate with maybe some added delay.

What you need to do is separate your code which communicates to with the server and the code which works on the UI. Take all the code which connects to the server and reads response, and put it directly in the doInBackground()method without any runInUI() nonsense. You have bits of code which you need to run in the UI to update the spinner and associated adapter. I would suggest you create the adapter and associate with the spinner in the onCreate() method. Then in the AsyncTask, implement the onProgressUpdate(String... values) method.

For the onProgressUpdate() method, take any of the values you get as a parameter, add them to the adapter, and tell the adapter its values have changed (so the spinner updates). The onProgressUpdate() method gets called in the UI thread in response to you calling publishProgress(String... values). So your doInBackground() method would communicate to the server and read the response, turn the response into Strings you can use, and then call publishProgress() with those Strings. publishProgress() (implemented by Android, not something you have to write) moves those Strings to the UI thread and calls onProgressUpdate() with an accumulation of the values, and onProgressUpdate() (which you do write) puts those values into the adapter.

See this API: http://developer.android.com/reference/android/os/AsyncTask.html and don't forget to look on the developer's page for example uses of AsyncTask, I would be very surprised if there isn't an example that closely matches what you need to do.

It may still take a while for the complete list to fill out, if the communication with the network takes 6 seconds there is little you can do on the client to make it happen quicker. But at least in those 6 seconds your application will be usable and responsive.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic