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;

            // 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){

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

    public void pauseApp() {

    public void destroyApp(boolean unconditional) {
Tags: , ,
mahmud ahsan

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Github | Business | Twitter | Linkedin | Instagram | 500px]

You may also like


  • 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

  • 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
      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.

  • PrellZex
    December 13, 2009 at 3:33 am

    eh… thanks for post :))

  • rahul
    February 4, 2011 at 1:03 pm

    @mahmud ahsan… tnx bro 4 da code.. 🙂

Comments are closed here.

About Me

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Github | Business | Twitter | Linkedin | Instagram | 500px]


Working life at home Muaz < 4 Months Muaz and Jayed First Smile Little Angel Trees Horse Ride Maritime Museum in Malacca Dome of Blue Mosque Malaysia

Twitter Feed