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
New Contributor
ERKD
Posts: 7
Registered: ‎05-14-2011
My Device: Not Specified
Accepted Solution

Help!!!! its very urgent .....I got struck in connector.open() [New]

[ Edited ]

Hello guys,

 

This my first post, I hope you guys will solve my problem. I am creating a client-server application where data will exchange on Wifi. Every thing is working fine. But as I open   and close the single connection( as my application demand this), after working continuously for long hours  the IOException occur.

 

 

con = Connector.open(url,Connector.READ_WRITE); // Error

                connection = (StreamConnectionNotifier)con;
                sc = (StreamConnection) connection.acceptAndOpen();

 

I open this connection in different thread not in main.

 

Please help me.....

Please use plain text.
Developer
peter_strange
Posts: 19,598
Registered: ‎07-14-2008
My Device: Not Specified

Re: Help!!!! its very urgent .....I got struck in connector.open() [New]

I would expect there to be the occasional glitch in WiFi service, is that all that is happening?  Can you restart the processing once WiFi is back?

 

What is the URL you are using for the open?

Please use plain text.
New Contributor
cultofdanurag
Posts: 7
Registered: ‎02-04-2013
My Device: BlackBerry Curve 9360
My Carrier: Airtel

Re: Help!!!! its very urgent .....I got struck in connector.open() [New]

Check your URL, and wifi connections.

If still the problem persists 
drop the "stacktrace" for the IOException. It might help in debugging your problem. 

suggestion: use try, catch blocks

Please use plain text.
New Contributor
ERKD
Posts: 7
Registered: ‎05-14-2011
My Device: Not Specified

Re: Help!!!! its very urgent .....I got struck in connector.open() [New]

I am using this URL

String url = "socket://:44444;deviceside=true;interface=wifi";

 

I am using the try and catch blocks...

Please use plain text.
New Contributor
ERKD
Posts: 7
Registered: ‎05-14-2011
My Device: Not Specified

Re: Help!!!! its very urgent .....I got struck in connector.open() [New]

Yes Peter you are right...there is a glitch in Wi-Fi service.my IP is not the static one, it switches between two modem. Is this possible that this might be the reason?? I exit from the application (System.exit(0)) by closing all connection once I found exception in the line

sc = (StreamConnection) connection.acceptAndOpen();
Please use plain text.
Developer
peter_strange
Posts: 19,598
Registered: ‎07-14-2008
My Device: Not Specified

Re: Help!!!! its very urgent .....I got struck in connector.open() [New]

I agree that the switching is likely to be causing your problem.  I would you use your try/catch to catch the problem and then loop to restart the processing.  I suggest you put a sleep in the loop otherwise you will kill the battery.  You might also like to check WiFi status and not do the open when there is no WiFi.

Please use plain text.
New Contributor
ERKD
Posts: 7
Registered: ‎05-14-2011
My Device: Not Specified

Re: Help!!!! its very urgent .....I got struck in connector.open() [New]

Ok..Peter. I will apply the same and will get back to you.

Please use plain text.
New Contributor
ERKD
Posts: 7
Registered: ‎05-14-2011
My Device: Not Specified

Re: Help!!!! its very urgent .....I got struck in connector.open() [New]

Hello,

 

I am still getting the same exception...Actually on debugging I found the  IOException occuring in following code then on restarting the app again it won't open Connector.open(). And the weired thing is, it doesn't  happens all time. So please could you eloborate the optimal way to close the connection and all the stuffs.Right now, I am doing

 

try{

                connection = (StreamConnectionNotifier)con;
                sc = (StreamConnection) connection.acceptAndOpen();
            }
            catch(Exception e) // Unable to connect
            {
                System.out.println("Unable to connect222..................."+e.getMessage()); //
                e.printStackTrace();
                isConnected=false;
               
                invokeAndWait( new Runnable()
                {
                    /**
                     * @see java.lang.Runnable#run()
                     */
                    public void run()
                    {
                        try {
                            if(SmsSenderReceiver.messconn!=null){
                                SmsSenderReceiver.messconn.close();}

                        } catch (IOException e) {

                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                
                        if(sc != null)
                        {
                            try{
                                sc.close();
                            }  catch(IOException ioe)
                            {
                                ioe.printStackTrace();                
                            }

                        }
                   
                        if ( connection != null)
                        {
                            try
                            {
                                connection.close();
                            }
                            catch(IOException ioe)
                            {  
                                ioe.printStackTrace();
                            }
                        }
                        System.exit(0);
                    }
                });
            }

Please use plain text.
Developer
peter_strange
Posts: 19,598
Registered: ‎07-14-2008
My Device: Not Specified

Re: Help!!!! its very urgent .....I got struck in connector.open() [New]

The first thing to note is that this processing should be on a separate Thread.  It looks like this code is in the class you have that extends UiApplication, so this might not be the case.

 

The second thing is that you shuld not close all the streams using an invokeLater.  This Thread should be running in the background and so you should be able to just close the streams.

 

Finaly you will not get a stack trace in an Exceptin like this.  So you can remove the e.printstacktrace(). 

 

Now here is pseudo code to describe how I would implement this:

 

boolean finished = false;

while ( !finished) {

try {

conn....

// real processing in here

} catch Exception e) {

System.out.prin....

if ( SMmsSender... ) {

....close();

}

// close all streams

Thread.sleep(5000);

// rest the connection for 5 seconds so thta it can get over the problem.

} // end of catch

 

} // end of while loop.

 

Hope this helps.

Please use plain text.
New Contributor
ERKD
Posts: 7
Registered: ‎05-14-2011
My Device: Not Specified

Re: Help!!!! its very urgent .....I got struck in connector.open() [New]

Thanks man...!!  you are genious. You have solved my problem. Sorry for the late reply, hope you won't mind..!!:smileywink:

Please use plain text.