A J2ME application that connects and read data from HTTP protocol

This example shows you how to connect and read data from a http protocol. When you implement it and run it from your mobile device, you have to change the url from localhost to a real hosting address. (Like http://www.temp.com/somefile.txt)

/*
* Midlet.java
* Created on March 14, 2007, 1:15 PM
* @author  mahmud
* @version
*/

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;

public class Midlet extends MIDlet {
    private Display display;
    private Form form;
    public void startApp() {
        display = Display.getDisplay(this);

        form = new Form('Http Example');
        StringBuffer buffer = null;

        try{
            // you could use other http://www.something.com/file.html as a example

            String url = 'http://localhost/shaon.txt';

            StreamConnection conn = (StreamConnection) Connector.open(url);
            InputStream in = conn.openInputStream();

            buffer = new StringBuffer();
            int ch;

            while ( (ch = in.read())!= -1){
            buffer.append( (char) ch);

            }
        }
        catch(Exception o){
            o.printStackTrace();
        }

        form.append(new StringItem(null, buffer.toString()));
        display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

About mahmud ahsan

Programming is my passion from high school. I always looking for innovating new ideas and making them real application. I love mobile application and game development as well as web development. Having 4+ years web development experience and 6+ years mobile application development experience on different platform and business on it. My Mobile Apps Portfolio: iThinkdiff.net Mobile Apps

, ,

5 Responses to A J2ME application that connects and read data from HTTP protocol

  1. Fuad December 25, 2008 at 9:46 am #

    Hello Mahmud,
    Can you just tell me how the content provider companies are doing their business? If you know then please tell me both from technical and business perspective. In terms of technical I mean,,, how are they connected with phone companies? are they using their own applications and server to manage the contents? or what? my email address is fuad@thevervegroup.com … you can email me… thank you

  2. acu November 13, 2009 at 2:33 pm #

    hye..i want to ask u about mobile application using netbeans. how to display data on textbox. i have entered the data in textField using simulator. besides that, how to connect midlet to database (Java DB)in mobile device without using servlet? it is necessary to use servlet?? i hope u can help me..you can email me.plz!!! thank you

    • mahmud ahsan November 15, 2009 at 9:59 pm #

      @Acu, I’m sorry to say that i’m not working on J2ME for last 1 and half years. So I don’t know about it.

  3. PrellZex December 13, 2009 at 3:33 am #

    eh… thanks for post :))

  4. rahul February 4, 2011 at 1:03 pm #

    @mahmud ahsan… tnx bro 4 da code.. :)