1) Create a new Android project
2) Add permission to access the Internet from your application to your AndroidManifest.xml
3) Create a new (static) class called HttpClient.java
4) Add the following code to your MainActivity.java
The full source code is also available on GitHub for download.
Check the official Android documentation for more information about JSONObjects and Apache's HttpClient classes.
I hope this tutorial was helpful for you. If it was too simple for your needs stay tuned. Upcoming tutorials will deal with the following topics:
- Creating a separate HttpClient thread to uncouple the hard work happening in the background from the user interface
- Dealing with HTTP exceptions: transport exceptions, protocol exceptions, timeouts.
- Setting up a BackgroundService and a ServiceInterface, which will be the new home for our HttpClient. This enables many activies to simply connect to the service and to access the resources provided instead of creating a new HttpClient object for each activity
- Instead of using a simple HttpClient (as shown in this tutorial), we will utilize ClientConnectionManager to provide us with a connection pool to save resources and improve performance