04-24-2014 10:46 AM
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.
Solved! Go to Solution.
04-24-2014 11:54 AM
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.
04-25-2014 05:51 AM
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
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?
04-25-2014 11:40 AM
Have a look at the example using POST you will find on this page.
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.
04-29-2014 07:03 AM
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).
04-30-2014 08:16 AM
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..
04-30-2014 10:48 AM
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:
05-08-2014 08:18 AM
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.