Async Networking in Android

Async Networking in Android

(Deprecated, I never use those technology anymore.) 

Table of Contents

  • Background
  • Source code examples

1 Background

A few days ago, people ask me a question, How to write an application which has to download the resources from the network? or Let me narrow the scope, how to write an android app, which can download the images from the Internet with HTTP protocol and insert the image into a view container like a ListView.

So, in this scenario, because the networking connections are always resources burden works, the solution is always put the heavy jobs into the background threads. Following is the equation to that problem.

Threads + Cache + HttpClient

I found three ways to met this equation after study that topic.
  1. Use AsyncTask(thread) directly, put the networking job into a background AsyncTask.
  2. Use ThreadPool, which is better than AsyncTask, especially in massive networking connection scenario.
  3. Use Volley library, which is advocated by Google, and its implementation details include the ThreadPool + Queue + Cache + HttpClient. And yes, it is the most fantastic solution, and of course the fastest one.
About the Cache mechanism, I just cache the image in the memory at this moment. And I chose the HTTPURLConnection as the default HTTP client.

2 Source code examples

I upload the source code into the following repository.
  AsyncTask + ThreadPool
  Volley

Comments

Popular posts from this blog

Bluedroid stack in android

How to setup a NAT server?

Network programming in elisp