This is another example of HTTP connection implementation using the classes HttpConnection and ConnectionManager. It uses Apache�s HttpClient methods and allows for GET, POST, PUT and DELETE requests. A built in Bitmap decoder is also included.
To receive status updates from an HttpConnection a Handler is used. The HttpConnection will dispatch a message to the Handler at the occurrence of the following:
This example also gets an image and text from the web. But unlike the previous example, we do not any buttons, and it will get the image and text automatically from a separate thread.
Here are the files used in this example.
HttpActivity.java:
package com.bogotobogo.httpconnectionb; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.ImageView; import android.widget.TextView; public class HttpActivity extends Activity { private ImageView image; private TextView text; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); image = (ImageView) findViewById(R.id.image); text = (TextView) findViewById(R.id.text); downloadTwitterIcon(); downloadTwitterStream(); } public void downloadTwitterIcon() { Handler handler = new Handler() { public void handleMessage(Message message) { switch (message.what) { case HttpConnection.DID_START: { Log.d("Image", "Starting Connection"); break; } case HttpConnection.DID_SUCCEED: { Bitmap response = (Bitmap) message.obj; image.setImageBitmap(response); break; } case HttpConnection.DID_ERROR: { Exception e = (Exception) message.obj; e.printStackTrace(); break; } } } }; new HttpConnection(handler).bitmap ("http://www.bogotobogo.com/images/HTML5/HTML5-Number5.png"); } public void downloadTwitterStream() { Handler handler = new Handler() { public void handleMessage(Message message) { switch (message.what) { case HttpConnection.DID_START: { text.setText("Starting connection..."); break; } case HttpConnection.DID_SUCCEED: { String response = (String) message.obj; text.setText(response); break; } case HttpConnection.DID_ERROR: { Exception e = (Exception) message.obj; e.printStackTrace(); text.setText("Connection failed."); break; } } } }; new HttpConnection(handler).get ("http://www.bogotobogo.com/HTML5/HTML5_Tutorial.html"); } }
HttpConnection.java:
package com.bogotobogo.httpconnectionb; import java.io.*; import org.apache.http.*; import org.apache.http.client.*; import org.apache.http.client.methods.*; import org.apache.http.entity.*; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.HttpConnectionParams; import android.graphics.*; import android.os.*; public class HttpConnection implements Runnable { public static final int DID_START = 0; public static final int DID_ERROR = 1; public static final int DID_SUCCEED = 2; private static final int GET = 0; private static final int POST = 1; private static final int PUT = 2; private static final int DELETE = 3; private static final int BITMAP = 4; private String url; private int method; private Handler handler; private String data; private HttpClient httpClient; public HttpConnection() { this(new Handler()); } public HttpConnection(Handler h) { handler = h; } public void create(int method, String url, String data) { this.method = method; this.url = url; this.data = data; ConnectionManager.getInstance().push(this); } public void get(String url) { create(GET, url, null); } public void post(String url, String data) { create(POST, url, data); } public void put(String url, String data) { create(PUT, url, data); } public void delete(String url) { create(DELETE, url, null); } public void bitmap(String url) { create(BITMAP, url, null); } public void run() { handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START)); httpClient = new DefaultHttpClient(); HttpConnectionParams.setSoTimeout(httpClient.getParams(), 25000); try { HttpResponse response = null; switch (method) { case GET: response = httpClient.execute(new HttpGet(url)); break; case POST: HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(data)); response = httpClient.execute(httpPost); break; case PUT: HttpPut httpPut = new HttpPut(url); httpPut.setEntity(new StringEntity(data)); response = httpClient.execute(httpPut); break; case DELETE: response = httpClient.execute(new HttpDelete(url)); break; case BITMAP: response = httpClient.execute(new HttpGet(url)); processBitmapEntity(response.getEntity()); break; } if (method < BITMAP) processEntity(response.getEntity()); } catch (Exception e) { handler.sendMessage(Message.obtain(handler, HttpConnection.DID_ERROR, e)); } ConnectionManager.getInstance().didComplete(this); } private void processEntity(HttpEntity entity) throws IllegalStateException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(entity .getContent())); String line, result = ""; while ((line = br.readLine()) != null) result += line; Message message = Message.obtain(handler, DID_SUCCEED, result); handler.sendMessage(message); } private void processBitmapEntity(HttpEntity entity) throws IOException { BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent()); handler.sendMessage(Message.obtain(handler, DID_SUCCEED, bm)); } }
ConnectionManager.java:
package com.bogotobogo.httpconnectionb; import java.util.ArrayList; public class ConnectionManager { public static final int MAX_CONNECTIONS = 5; private ArrayList active = new ArrayList(); private ArrayList queue = new ArrayList(); private static ConnectionManager instance; public static ConnectionManager getInstance() { if (instance == null) instance = new ConnectionManager(); return instance; } public void push(Runnable runnable) { queue.add(runnable); if (active.size() < MAX_CONNECTIONS) startNext(); } private void startNext() { if (!queue.isEmpty()) { Runnable next = queue.get(0); queue.remove(0); active.add(next); Thread thread = new Thread(next); thread.start(); } } public void didComplete(Runnable runnable) { active.remove(runnable); startNext(); } }