Sunday, 22 January 2012


J2ME Program to Access Data from a Specific URL. The stream connection is used to connect the application to specific URL by Airtime.

Code:
/*J2ME Program to Access Data from a Specific URL*/

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

public class AccessUrl extends MIDlet
{
  private Display display;
  String url = "http://localhost/AccessURL/hello.txt";
  public AccessUrl()
  {
    display = Display.getDisplay(this);
  }
  public void startApp()
{
    Try
{
      connection(url);
    }
catch (IOException e)
{
      System.out.println("IOException " + e);
      e.printStackTrace();
    }
  }

  public void pauseApp(){}
  public void destroyApp(boolean unconditional){}
  void connection(String url) throws IOException
{
    StreamConnection sc = null;
    InputStream is = null;
    StringBuffer buffer = new StringBuffer();
    TextBox access;
    Try
{
      sc = (StreamConnection)Connector.open(url);
      is = sc.openInputStream();
      int chars;
      while((chars = is.read()) != -1)
{
        buffer.append((char) chars);
      }
      System.out.println(buffer.toString());
      access = new TextBox("Access Text", buffer.toString(), 1024, 0);
    }
Finally
{
      if(is != null)
      {
        is.close();
      }
      if(sc != null)
       {
        sc.close();
      }
    }
    display.setCurrent(access);
  }
}

Sample Output:

Comment bellow for Query and Feedback :)

Related Posts :



0 comments:

Confused? Feel free to ask

Post a Comment