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: 15
Registered: ‎09-01-2008
My Device: Not Specified

IllegalStateException when trying to do simple httpconnection

[ Edited ]

I have read many many threads, pdf's, and even a chapter of a book, and I cannot figure out why my code is not preforming a simple http GET. I am trying to get it working on the simulator. This code is really a mild elaboration of the popular httpconnect example. I am running MDS (Edit->Preferences->Simulator->General->Run MDS checkbox). I also have set Application Permissions to Custom Access and Allow All for Connections, Interactions, and User Data. The URL I am retrieving is:

http://www.tonyamoyal.com/test.xml

I have also tried it with

http://www.tonyamoyal.com/test.xml;deviceside=false

and

http://www.tonyamoyal.com/test.xml;deviceside=true

 

because I am not clear on the difference yet. I am going to post my code below because right now it is in one file so it should be easy to look at. Any help would be AMAZING. Thank you in advance.

package com.mypackage;

import java.io.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.i18n.*;
import net.rim.device.api.system.*;
import javax.microedition.io.*;
import java.io.*;

public class BBMusic extends UiApplication {
   
    public static void main(String[] args){
        BBMusic app = new BBMusic();
       
        //set object to listen for events
        app.enterEventDispatcher();
    }
   
    public BBMusic(){
        //display the main BBMusic screen
        pushScreen(new BBMusicMainScreen());
    }
}


final class BBMusicMainScreen extends MainScreen{

    //constants
    private static final String SAMPLE_PAGE = "http://www.tonyamoyal.com/test.xml;deviceside=false";
   
    //members
    private RichTextField _content;
   
    ConnectionThread _connectionThread = new ConnectionThread();
   
    public BBMusicMainScreen(){
        //invoke MainScreen constructor
        super();
       
        LabelField title = new LabelField("Welcome to BBMusic v0.0.15" , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
        setTitle(title);
     
        add(new RichTextField("Select Your Playlist:"));
        add(new SeparatorField());
       
        Object[] playlistChoices;
        playlistChoices = new Object[3];
        playlistChoices[0] = "My first playlist";
        playlistChoices[1] = "My second playlist";
        playlistChoices[2] = "My third playlist";
        ObjectChoiceField PlaylistSelector = new ObjectChoiceField("", playlistChoices, 0);
        add(PlaylistSelector);
       
        add(new SeparatorField());
       
        ButtonField btn = new ButtonField("Click It");
        btn.setChangeListener(submitListener);
        add(btn);
       
        add(new SeparatorField());

        _content = new RichTextField("HTTP RESPONSE: ");
        add(_content);
       
    }
   
    FieldChangeListener submitListener = new FieldChangeListener(){
        public void fieldChanged(Field field, int context){
            init();
        }
    };
   
   
    private void init(){
        _connectionThread.start();
        fetchPage(SAMPLE_PAGE);
    }
   
    private void fetchPage(String url){
        url = url.toLowerCase();
        _connectionThread.fetch(url);
    }
  
    private MenuItem _closeItem = new MenuItem("Close", 200000, 10){
        public void run(){
            Dialog.alert("Close Item closing");
            onClose();
        }
    };
   
    private MenuItem _cancelItem = new MenuItem("Cancel", 200000, 9){
        public void run(){
        }
    };     
   
    protected void makeMenu(Menu menu, int instance){
        menu.add(_closeItem);
        menu.add(_cancelItem);
    }
   
    public boolean onClose(){
        Dialog.alert("Goodbye!");
        _connectionThread.stop();
        System.exit(0);
        return true;
    }
   
    private void updateContent(final String text){
        _content.setText(text);
    }
   
    private class ConnectionThread extends Thread{
        private static final int TIMEOUT = 500; //ms
   
        private volatile boolean _start = false;
        private volatile boolean _stop = false;
   
        private String _url;
   
        public void fetch(String url){
            _start = true;
            _url = url;
        }
   
        public void stop(){
            _stop = true;
        }
       
        public synchronized String getUrl(){
            return _url;
        }      
   
        public void run(){
            for(;Smiley Wink{
                //while no connections are open and thread has not been stopped
                //put thread to sleep to save cpu cycles
                while( !_start && !_stop ){
                    try{
                        sleep(TIMEOUT);
                    }catch (InterruptedException e){
                        System.err.println(e.toString());
                    }
                }
               
                if(_stop) return; //exit thread on stop
               
                //at this point _start has to be true so we process request
                synchronized(this){
                   
                    //open the connection
                    HttpConnection c = null;

                    try{
                        c = (HttpConnection)Connector.open(getUrl());
                       
                        InputStream istream = c.openInputStream();
                       
                        byte[] data = new byte[256];
                        int len = 0;
                        StringBuffer raw = new StringBuffer();
                       
                        //grab data in 256 byte chunks
                        while( -1 != (len = istream.read(data)) ){
                            raw.append(new String(data, 0, len));
                        }
                       
                        //convert data to a string
                        String text = raw.toString();
                        //display the string
                        updateContent(text);
                        istream.close();
                        c.close();
                       
                    } catch (IOException e){
                        System.err.println(e.toString());
                        updateContent(e.toString());
                    }
                    //indicate the connection is finished
                    _start = false;
                }
            }//end infinite loop 
        }//end run()   
    }  
}

Message Edited by corgan1003 on 09-03-2008 12:38 AM
Developer
Posts: 16,986
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: IllegalStateException when trying to do simple httpconnection

your updateContent method has to synchronize on the eventlock.
use either synchronized(getEventLock()) or invokeLater.

See the developers guide for more details about this mechanism.
you can find them here:
http://na.blackberry.com/eng/support/docs/subcategories/?userType=21&category=BlackBerry+Java+Application+Development&subCategory=BlackBerry+Java+Development+Environment
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Developer
Posts: 15
Registered: ‎09-01-2008
My Device: Not Specified

Re: IllegalStateException when trying to do simple httpconnection

Thank you!
New Developer
Posts: 15
Registered: ‎09-01-2008
My Device: Not Specified

Re: IllegalStateException when trying to do simple httpconnection

oh and for those who still cannot get this to work.  deviceside actually has to be = true for simulator
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: IllegalStateException when trying to do simple httpconnection

Both deviceside=true and deviceside=false will work from the simulator.  When using deviceside=false, you are emulating a device with no direct TCP connectivity (hence deviceside (tcp stack) is set to false).  This means the connection to the internet must be provided by the MDS, so you need the MDS Simulator running.  It is often useful to have the MDS Simulator running, especially when testing apps you are developing for the BES/MDS environment, but also so that the Browser on the Simulator works.

 

You will find more information here:

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=1765&query.id=104...

New Developer
Posts: 15
Registered: ‎09-01-2008
My Device: Not Specified

Re: IllegalStateException when trying to do simple httpconnection

I see, well I do have the MDS simulator running, so now I am a bit confused....thanks for the info
New Developer
Posts: 15
Registered: ‎09-01-2008
My Device: Not Specified

Re: IllegalStateException when trying to do simple httpconnection

Yea I have done some more tests and I am always getting timeouts when deviceside=false; 

 

Do I have to do more than go to Edit-->Preferences-->Simulator-->Launch MDS-CS with simulator?

 

Thank you! 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: IllegalStateException when trying to do simple httpconnection

In theory no, but this assumes that the MDS actually starts!  When I'm running the MDS, I see a Command Line window which displays what looks like a log - you can see activity whenever you do something that uses it on the simulator.

 

I would start it manually and make sure it is working - i.e. you see the Command Line Window.  Then try the Browser on the Sim to check it can get through.

 

Any problems, here are two KB articles that might help.

 

Support - BlackBerry Mobile Data Service Simulator does not launch
Article Number: DB-00054

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800738/800792/801079/...

 

How To - Configure the BlackBerry MDS simulator to work behind a proxy
Article Number: DB-00047

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800738/800792/801079/...

New Member
Posts: 2
Registered: ‎09-04-2010
My Device: web developer (4.6, 5, 6)

Re: IllegalStateException when trying to do simple httpconnection

I cannot even manually launch the browser from within the 9800 simulator running on Windows 7. I get

 

JVM Error 104

Uncaught:

IllegalStateException

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: IllegalStateException when trying to do simple httpconnection

That is not good!

 

I'm not on Win 7, but others in this forum are, so hopefully you will get more responses.  In the mean time:

 

1) Have you tried other Simulators?

2) Are you running 32 bit Java?