Home J2me Access URL

Related Tutorials


 
 

Share on Google+Share on Google+

Access URL

Advertisement
This example is used to access the data from the specific url. The Stream Connection is used to connect the application to the specific url by Airtime (connect to the internet).

Access URL

     

This example is used to access the data from the specific url. The Stream Connection is used to connect the application to the specific url by Airtime (connect to the internet). In this example we are trying to make a connection between internet and mobile. In the example we have created the connection, but here a message will be shown "Is it OK to Use Airtime?", If user click on 'No' button then it return to launch page, but if user click on 'Yes' button then it check the http connection from the internet, if connection is available then text will be displayed otherwise exception will be thrown.

 

 

 

 

The Application is as follows:

 

Source Code of AccessUrl.java

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://www.roseindia.net/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(), 10240);
  }finally{
  if(is != null){
  is.close();
  }
  if(sc != null){
  sc.close();
  }
  }
  display.setCurrent(access);
  }
}

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 18, 2008

Related Tutorials

Discuss: Access URL   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
Dima
October 27, 2012
Works on emulator but doesn't on real device

Hi, I can see that it works on emulator, but it can't connect to the internet on my mobile. On the real device I have Opera browser which works well via GRPS, but this midlet does't get any data
Khadim
October 28, 2012
Adding commands

Hi, I want to know how to add commans to this codes. For exemple a back command to return to a menu.
valuba
March 6, 2013
null pointer exception

hello. thanks for the tutorial. i tried implementing your source code on my j2me application. and when i ran it, it displayed a "java/lang/nullpointerexception"
DMCA.com