A Simple Http Connection Handler for J2ME

This is a Http(s) connection handler & can be used in Java enabled mobile devices. In this you may find some classes like “Logger”, “DisplayStack”, “Connectable” blah blah blah. Just forget about it. Afaik, this code has to work. If you find any flaws in this code, please lemme know.

package subin.rnd.network;
import java.io.InputStream;
import javax.microedition.io.Connector; 
import javax.microedition.io.HttpConnection; 
import javax.microedition.lcdui.Alert; 
import javax.microedition.lcdui.AlertType;
import subin.rnd.midlet.Launcher.Connectable; 
import subin.rnd.util.DisplayStack; 
import subin.rnd.util.Logger;
public class HttpConnector 
{ 
    public static void get(String url, Connectable connectable) 
    { 
        new Thread() 
        { 
            public void run() 
            { 
                Alert waitAlert = new Alert(null, "Connecting...", null, AlertType.INFO); 
                DisplayStack.push(waitAlert); 
            } 
        }.start(); 
        
        new Thread() 
        { 
            String url;            
            Connectable connectable; 
            
            Thread setParam(String url, Connectable connectable) 
            { 
                this.url = url; 
                this.connectable = connectable; 
                return (this); 
            }   
            
            public void run() 
            { 
                Logger.print("Connecting to: " + this.url); 
                
                HttpConnection  conn            = null; 
                InputStream     inputStream     = null; 
                StringBuffer    responseData    = null;  
                
                try 
                { 
                    conn = (HttpConnection) Connector.open(this.url, Connector.READ); 
                    if (conn == null) 
                        throw new Exception("Unable to create connection object"); 
                    
                    conn.setRequestMethod(HttpConnection.GET); 
                    conn.setRequestProperty("user-agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0"); 
                    
                    if (conn.getResponseCode() != HttpConnection.HTTP_OK) 
                        throw new Exception("Unable to connect to " + this.url 
                                + " [Error code: " + conn.getResponseCode() + "]"); 
                    
                    inputStream = conn.openInputStream(); 
                    Logger.print("Response data length: " + inputStream.available()); 
                    
                    responseData = new StringBuffer(); 
                    int aCharFromStream; 
                    while ((aCharFromStream = inputStream.read()) != -1) 
                        responseData.append((char) aCharFromStream); 
                  
                    DisplayStack.pop(); 
                    this.connectable.processResponse(responseData.toString().getBytes());                    
                } 
                catch (Exception e) 
                { 
                    DisplayStack.pop(); 
                    Logger.print((e.getMessage() != null) ? e.getMessage() : "Exception", Logger.ERROR); 
                }                
                finally 
                { 
                    try 
                    { 
                        if (conn != null)           conn.close(); 
                        if (inputStream != null)    inputStream.close(); 
                    } 
                    catch (Exception e) {}                    
                } 
            } 
        }.setParam(url, connectable).start(); 
    } 
}
Advertisements