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
laety_Dev
Posts: 12
Registered: ‎04-24-2014
My Device: Developer
Accepted Solution

net.rim.device.api.io.ConnectionClosedException: Not connected

Hi everyone,

 

I am having trouble when making POST calls on my app.

 

When I first encounter the above issue, I fixed it by running the network call in a different thread.

However, the problem has started all over again, 

Everytime I make a POST request, my connection returns a response code 0, and I do not know what is causing the connection to close. Get request are working fine as well as one POST request when a user logs in.

However, eg when a user signs up, I get a response code 0.

 

I have been on it for a month now and I have read many post on this matter, and yet I have not found anything that solve my problem.

 

Can anyone assist me with this matter.

 

Thank you

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

Re: net.rim.device.api.io.ConnectionClosedException: Not connected

Welcome to the forums.

 

You can only use an Http connection once, so if you have done a POST and got a return code of 0, then you should not try to do anything more on that connection, for example, you shuld not try to send more data.  That will fail with connection closed exception.

 

So review your logic, I suspect you just need to use http connection correctly to fix this problem. 

Please use plain text.
New Developer
laety_Dev
Posts: 12
Registered: ‎04-24-2014
My Device: Developer

Re: net.rim.device.api.io.ConnectionClosedException: Not connected

Hi peter_strange,

 

Thank you for your quick response,

 

However, I am not sure I follow.

My logic is as follow:

create a http connection with connection = (HttpConnection) Connector.open(url + ";deviceside=true" + timeout);    

set requestmethod to Post

setProperty("Accept", "application/json");
setProperty("Content-type", "application/x-www-form-urlencoded");

open an output stream, and wrtie bytes of data in it

the issue occurs when I read from the connection using inputstream, that  is where the IOException is thrown, and response is 0.

and I do not try to Post data after the connection has closed.

 

So I still do not understand what is causing it to close either.

 

Would you have a clue?

 

Regards

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

Re: net.rim.device.api.io.ConnectionClosedException: Not connected

Have a look at the example using POST you will find on this page.

 

http://www.blackberry.com/developers/docs/7.1.0api/javax/microedition/io/HttpConnection.html

 

Compare your implementatoin with it and see what the difference is.  You should specifically look at when you get the response code and when you try to read the response data. 

Please use plain text.
New Developer
laety_Dev
Posts: 12
Registered: ‎04-24-2014
My Device: Developer

Re: net.rim.device.api.io.ConnectionClosedException: Not connected

Hi

 

Apologies for my delayed response.

 

I will have a look at the look and check whether my implementation follow the same logic. 

I ll post back once I have obtained some result(good or bad).

 

Thanks 

 

Please use plain text.
New Developer
laety_Dev
Posts: 12
Registered: ‎04-24-2014
My Device: Developer

Re: net.rim.device.api.io.ConnectionClosedException: Not connected

Hi peter_strange

 

I had a look my implementation of the httpconnection class and compared it to the one from the link you provided me. 

However, with the only difference occuring for setproperty method as I need to be reading json elements after making api calls, there no differences between the 2 set of codes. 

And  I noticed that the response code is already 0 before attempting to open the outputstream and yet connection does write to the outputstream but when I open the inputstream and check the response code to retrieve data from the server, I ran into the connection closed exception.

 

So far  I still don't have a solution to his issue..

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

Re: net.rim.device.api.io.ConnectionClosedException: Not connected

I woudl be confident that you code is different, becuase you say this:

"I noticed that the response code is already 0"

 

How many checks of the response code are there in the sample I pointed you at?

 

You should complete your writes before you get the response code.  Getting the response code means that the BlackBerry sends the data to the Server and gets a response from the Server.  So, after the getResponseCode(), if you try to do anything other than read the response, it will fail as you have described.

 

Please review the following Post, and you might find other posts in the Thread interesting:

 

http://supportforums.blackberry.com/t5/notifications/notifymoderatorpage/message-uid/2867136

Please use plain text.
New Developer
laety_Dev
Posts: 12
Registered: ‎04-24-2014
My Device: Developer

Re: net.rim.device.api.io.ConnectionClosedException: Not connected

Hi peter_strange

 

I have finally discovered what was the culprit in my code all along,  

given that the the http connection, and writing to the output stream and reading from the input stream was done in different functions it was hard to pinpout were the interruption was occuring. 

 

The issue was that I had a statement that says return call(), which was meant to return the true or false based on the inputstream successfully reading the data or failing to read from the connection. and somehow the program will not wait for response  from the server and will interrupt the connection and prevent reading from the inputstream and sometimes writing to the outputstream.

 

If I may, I would advise those using http connection to make sure that they implement it as in the rim documentation. splitting the connection, the input and output stream in different function make it hard to discover errors when they occur.

 

Thanks for your help Peter.

Please use plain text.