I’ve been trying to learn to to use Android SDK and have encountered my first issue of trying to send information to a database. My first though was to use POST in Android and send it to a PHP script. However every tutorial or example I’ve found has been in a version of Android lower than 3.0 meaning they don’t use AsyncTask and use Activity when extending their class. I am unsure how to write this code using ASyncTask and would like some advice or an example on how to do so, allowing it to work in Android 3.0 and onwards. Basically I want to be able to send variables to my php file on my website using Android Emulator.
Thanks for any help.
Just copy and paste your
HttpPostcode into theAsyncTask‘sdoInBackgroundmethod. You can notify the UI for progress updates usingonProgressUpdate, and you can notify the UI when the task is completed inonPostExecute.See this post for more information on how to implement the request (the code in the post performs an
HttpGetrequest, so translating the code to use aHttpPostrequest instead should be simple).And by the way, you should still use an
AsyncTaskto perform these kinds of requests on pre-HoneyComb devices. A lot of tutorials online do this incorrectly… just remember that you should always perform lengthy operations on a separateThread, and network connections should always be considered to be potentially long-running operations (since you never know how long they will take).