Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
New Developer
Posts: 10
Registered: ‎12-23-2009
My Device: 8900
My Carrier: Airtel

HttpConnection in J2ME for BlackBerry App Development

I was about to try one website access using J2ME for creating a BlackBerry App. The App is running  perfectly in BB Curve 8900 Simulator. But when i run it on the device, an error is generating. The Simple App shown below is having a home page having 2 commands: Google, and GMail. When we click on the command, its appropriate website will be loaded using HttpConnection class. And the return will be displayed in an Alert.

 

Please help me in this, that how can i get the Internet access in my application.

I've already activated BB Service and all, and also getting internet access properly in Browser.

 

 

import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;

public class Midlet extends MIDlet implements CommandListener
{
    Display display=null;
    Form frm=null;

    public void startApp()
    {
        display=Display.getDisplay(this);
        frm=new Form("Sample");
        frm.addCommand(new Command("Google", Command.SCREEN, 1));
        frm.addCommand(new Command("GMail", Command.SCREEN, 1));
        frm.setCommandListener(this);
        display.setCurrent(frm);
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable d)
    {
        String str=c.getLabel();
        if(str.equals("Google"))
            connectionMGR("http://www.google.com");
        else if(str.equals("GMail"))
            connectionMGR("http://www.gmail.com");
    }

    public void connectionMGR(final String url)
    {
        new Thread()
        {
            public void run()
            {
                try
                {
                    /*Alert al=new Alert("URL", url, null, AlertType.INFO);
                    al.setTimeout(Alert.FOREVER);
                    display.setCurrent(al, frm);*/
                    HttpConnection connection=(HttpConnection) Connector.open(url);
                    InputStream in=connection.openInputStream();
                    int ch=0;
                    String str="";
                    while((ch=in.read())!=-1)
                        str+=(char) ch;
                    Alert al=new Alert("Return", str, null, AlertType.INFO);
                    al.setTimeout(Alert.FOREVER);
                    display.setCurrent(al, frm);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    Alert al=new Alert("Error", ""+e, null, AlertType.ERROR);
                    al.setTimeout(Alert.FOREVER);
                    display.setCurrent(al, frm);
                }
            }
        }.start();
    }
}

 

Thanks in advance.

Developer
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: HttpConnection in J2ME for BlackBerry App Development

[ Edited ]

Check out the following stickied article:

 

Connecting your BlackBerry - http and socket connections to the world

 

And this article from RIM:

 

What Is - Different ways to make an HTTP or socket connection

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful