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 Developer
jwhanes
Posts: 18
Registered: ‎08-13-2008
My Device: Not Specified
Accepted Solution

Weird Connection Problem

This occurs on the simulator and actual device.  I've done this on 8800, 8703e, 8320, and 7100i.  OS 4.1 and 4.2.  Also tried on multiple carriers.  Tried with and without a BES.

 

I am initially able to create an http connection, and everything works just fine.  I connect, I receive my data.  Great. 

However, when I exit my application and go to the Blackberry desktop and toggle the wireless connection off and on then re-enter my application and try to do the same connection it will not work.

 

When running the debugger it seems to  try and excecute the 'int status = httpConn.getResponseCode();'

line of code and will not go any further.  It will just time out.  It actually gets caught by the try catch statement.  The Exception toString is 'java.io.InterruptedIOException: Local connection timed out after ~ 120000'

 

My code to connect is..

 

 

StreamConnection s = null; String finalUrl = ""; finalUrl = "http://" + login.url + directoryPath + filename + local.getUrlPlus(); try { s = (StreamConnection) Connector.open(finalUrl); HttpConnection httpConn = (HttpConnection) s; httpConn.setRequestMethod(HttpConnection.POST); httpConn.setRequestProperty("user-agent", "BB DOC"); httpConn.setRequestProperty("content-type", "application/x-www-form-urlencoded"); httpConn.setRequestProperty("connection", "close"); byte d[] = data.getBytes(); httpConn.setRequestProperty("content-length", Integer.toString(d.length)); OutputStream os = s.openOutputStream(); os.write(d); if (os != null) os.close(); int status = httpConn.getResponseCode(); if (status == HttpConnection.HTTP_OK) { InputStream input = s.openInputStream(); //parses the content returned input.close(); } else { ;//Had a server connection problem } s.close(); httpConn.close(); } catch (IOException e) { ;//Catches the timeout here. }

 

 

 

 

Seems the only way to fix this is to restart the simulator.

 

Only occurs when the wireless is toggled off and on.  Its like something doesn't get reinitialized or something.  I've simulated this when the device is made out of coverage and brought back in coverage, but the error never occured.  Only when wireless is toggled off and on.

Whats even more weird, is I've been using the same connection code for over a year without any problems. 

 

Anyone ever experienced this?  Its driving me insane. 

 

Any help would be more then appreciated.  Thanks! 

Please use plain text.
New Developer
jwhanes
Posts: 18
Registered: ‎08-13-2008
My Device: Not Specified

Re: Weird Connection Problem

Wow that code was not formatted well at all.  Let me retry that...

 

 

StreamConnection s = null;

String finalUrl = "";

finalUrl = "http://" + login.url + directoryPath + filename + local.getUrlPlus();

 

try {

s = (StreamConnection) Connector.open(finalUrl);

HttpConnection httpConn = (HttpConnection) s;

 

httpConn.setRequestMethod(HttpConnection.POST);

httpConn.setRequestProperty("user-agent", "BB DOC");

httpConn.setRequestProperty("content-type", "application/x-www-form-urlencoded");

httpConn.setRequestProperty("connection", "close");

 

byte d[] = data.getBytes();

httpConn.setRequestProperty("content-length", Integer.toString(d.length));

 

OutputStream os = s.openOutputStream();

os.write(d);

 

if (os != null)

os.close();

 

int status = httpConn.getResponseCode();

 

if (status == HttpConnection.HTTP_OK) {

InputStream input = s.openInputStream();

//parses the content returned

input.close();

} else {

;//Had a server connection problem

}

 

s.close();

httpConn.close();

 

} catch (IOException e) {

;//Catches the timeout here.

}

 

 

 

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

Re: Weird Connection Problem

The weird formatting probably happened when you spell check.  Happened to me once, now I don't add code till I've done the spell check.

 

The following code (edited a bit, hope I didn't take anything out that was significant) carried on after a wireless off/on in my 4.1 simulator.  Not sure there is anything significant different, but you might notice something.  If you find out, let us know!

 

 

c = (HttpConnection)Connector.open(_connectionURL); c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0"); c.setRequestProperty("Content-Language", "en-US"); c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // code to create POST data in sb StringBuffer removed byte [] postBytes = (sb.toString()).getBytes(); c.setRequestProperty("Content-Length", Integer.toString(postBytes.length)); c.setRequestMethod(HttpConnection.POST); os = c.openOutputStream(); os.write(postBytes); os.flush(); rc = c.getResponseCode();

 

 

Please use plain text.
New Developer
jwhanes
Posts: 18
Registered: ‎08-13-2008
My Device: Not Specified

Re: Weird Connection Problem

Thanks very much for the reply.

 

I tried out your code, and it pretty much did the same thing.  Only this time it timed out on the 'os.flush' part.  When I removed this line (since it is optional according to the documentation) it timed out on the c.getResponseCode().  

 

Which makes sense I guess since c.getReponseCode() supposedly flushes, opens the connection, sends the request, and reads the response headers.

 

So it sounds like the problem is within the flushing of the output stream.  But why in the world would it time out?!?!  I'm a little lost.

 

Thanks again

 

Please use plain text.
New Developer
jwhanes
Posts: 18
Registered: ‎08-13-2008
My Device: Not Specified

Re: Weird Connection Problem

Well the problem has been narrowed down to the 8800 OS 4.2 and 8800 Simulator.  I did another test on my 8703e OS4.1 and 8703e OS4.2 and the problem didn't occur.  However on the 8800 when you hit 'Tun Off All Connections' then turn them back on the connection will time out.  

I swear my original test cases with those other devices didn't work, but I guess it may have been for a different reason.  But it doesn't work on the 8800!  Seriously!  Does anyone wanna give it a shot just for giggles?

 

If not no big deal.

 

Thank you. 

Please use plain text.
BlackBerry Development Advisor
MSohm
Posts: 14,677
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook

Re: Weird Connection Problem

[ Edited ]

Are other services able to communicate over the wireless network after you toggle the radio on and off?  Are you able to use the BlackBerry Browser and send and receive email?  Does the BlackBerry show EDGE coverage?

 

And just to verify, are you closing and re-opening your application after you have toggle wireless off and back on?

 

I recommend having a look at this link to ensure that your connections are closed.

 

How To - Close connections 
Article Number: DB-00530

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/How_To_...

Message Edited by MSohm on 09-15-2008 09:27 AM
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
New Developer
jwhanes
Posts: 18
Registered: ‎08-13-2008
My Device: Not Specified

Re: Weird Connection Problem

Thank you for the response.  I took a look at the article you posted and everything looks like its opening and closing properly.  And my application is being closed and reopened when I toggle the wireless off and on.

 

One interesting thing that you mentioned is the browser will not work either.

 

I start the simulator,

open up the browser,

go to some page (it shows up just fine)

clear the cache

exit browser,

go to Manage Connections,

Select 'Turn Off All Connections'

Then select "Restore Connections"

Reopen Browser,

Attempt to go to same page, and nothing happens.  Just times out.

 

The Blackberry does show edge coverage.

 

Thanks again for the help.

 

Please use plain text.
New Developer
jwhanes
Posts: 18
Registered: ‎08-13-2008
My Device: Not Specified

Re: Weird Connection Problem

Some additional information..

 

I've actually tried this on multiple simulators on different computers.  Even with my app uninstalled the browser shows the same issue. 

Please use plain text.
BlackBerry Development Advisor
MSohm
Posts: 14,677
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook

Re: Weird Connection Problem

This appears to be a BlackBerry Simulator issue.  I have been able to reproduce it and verified that it was fixed in version 4.3.0 of the BlackBerry Simulator.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
New Developer
jwhanes
Posts: 18
Registered: ‎08-13-2008
My Device: Not Specified

Re: Weird Connection Problem

YEAH!  So I'm not crazy!  Thanks for looking into it!
Please use plain text.