Mike Szczys · May 18, 2013 at 10:00 am

HttpClient Tutorial to Upload and Download with Your App

Developers wanting to interact with the Internet need to choose a package to do the heavy lifting for them. A very common route is to use the built-in HttpClient. The documentation is fairly complete, but it’s a daunting to get started if you’ve never used the package before.

Take a look at the HttpClient tutorial that JFrankie recently posted.  I find his presentation very approachable for the beginner, yet he doesn’t hesitate to move on to the more complex subjects. The guide starts off with a few lines of code used to open a connection, and quickly covers the first gotcha. HttpClient shouldn’t be run in the UI thread, as it presents a blocking function (you don’t know how long it’s going to be before the server answers back). Android will throw an error automatically if you try to do this.

He goes on to wrap the code in an AsyncTask class. This lets it run in a separate thread, launching a method when its work is complete. Most likely you’re looking to download something, a topic which is well covered in the guide. JFrankie wraps up the post by explaining the steps necessary to upload using the HTTP multipart request protocol.

He publicized his article in a Reddit thread, where he also participated in a discussion of the memory leak potential of using AsyncTask. It’s some advice you may want to heed as you build your app around the HttpClient.


_________
Want something on the XDA Portal? Send us a tip!

Mike Szczys

szczys is an editor on XDA-Developers, the largest community for Android users. Mike Szczys is a professional musician but spends all his free time working with hobby electronics. As Contributing Editor for Hackaday.com he became interested in Android as some of the early hardware hacks started popping up on the Internet. What followed was a gradually rising addiction to all things Android. View szczys's posts and articles here.
Mathew Brack · May 28, 2015 at 09:59 pm · 4 comments

I/O Summary: How Android M Handles Power And Charging

In the spirit of improving the core Android experience, Google is changing Android M to be smarter about managing power. Their new Doze feature comprises of two primary roles which allow Android to use motion detection in order to predict activity, and go into deep sleep at the right time based on accelerometer readings.   In order to extend your screen off battery life, Android M will now monitor your activity levels and if it detects that your device has...

XDA NEWS
Mario Tomás Serrafero · May 28, 2015 at 09:51 pm · 3 comments

I/O Summary: Google Photos App

At I/O 2015, Google tackled the information problem in mobile once more, this time through pictures: “how incredible is it that we all have a camera in our pockets at any moment? (...) These moments tell your story (...)[but] taking more pictures and videos makes it harder to relieve memories due to the sheer volume”. This is why they are revamping Google Photos and centering it around 3 big ideas:   Creating a home for all photos and videos that is...

XDA NEWS
Mario Tomás Serrafero · May 28, 2015 at 09:26 pm · 4 comments

I/O Summary: Development, Play Store, The Next Billion

VP of Engineering Jen Fitzpatrick began talking about what Google is doing to help “the next billion” come online. More and more people are getting their first smartphone, and for many people this first phone will be their first computer.   The majority of the next billion will be Android users and they “want to remove the barriers of smartphone adoption”. While there are huge displays of phones on sale, not all are able to run the latest and greatest...

XDA NEWS
Share This