Skip to content

J2ME program for url access

Aim: –Write a program to access data from a specific URL.      The stream connection is used to connect the application to specific URL by Airtime.
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);
  }
}
Output:-


Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!