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
Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Carrier: Vodafone
Accepted Solution

HttpPost- not getting response

I am calling web service using HTTP POST. But i am not getting response. Here is code.

public static String getPostData()
{
String url = "http://services.ex.samples.com/doAct.aspx";
InputStream _is = null;
byte[] postData = null ;
StringBuffer raw = new StringBuffer();
String retVal = "";
if(CoverageInfo.isOutOfCoverage())
{
return null;
}
try
{
HttpConnection httpConnection = (HttpConnection) Connector.open(url);
httpConnection.setRequestMethod(HttpConnection.POST);

httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);

encPostData.append("str1", "ABC");
encPostData.append("strCode", "");

encPostData.append("abcd", "0");
encPostData.append("strc", "ABCD");

encPostData.append("strParam1", "");
encPostData.append("strParam2", "");

encPostData.append("strParam3", "");
encPostData.append("strParam4", "");

encPostData.append("strParam5", "");
encPostData.append("strParam6", "");

encPostData.append("strParam7", "");
encPostData.append("strParam8", "");

encPostData.append("strParam9", "");
encPostData.append("strParam10", "");

postData = encPostData.toString().getBytes("UTF-8");
httpConnection.setRequestProperty("Content-Length", String.valueOf(postData.length));

OutputStream os = httpConnection.openOutputStream();
os.write(postData);

_is = httpConnection.openInputStream();
int len = (int)httpConnection.getLength();
int size = 0;
while ( -1 != (len = _is.read(postData)) )
{
raw.append(new String(postData, 0, len));
size += len;
}
retVal = raw.toString();
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
return retVal;
}

Please use plain text.
Developer
peter_strange
Posts: 19,506
Registered: ‎07-14-2008

Re: HttpPost- not getting response

So what does happen? 

What Exception is thrown? 

What connection method are you using (see http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-make-an-HTTP-or-socket-con...)

Does your Server see the request?

Please use plain text.
Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Carrier: Vodafone

Re: HttpPost- not getting response

I am getting response code 0, and rersponse message null. I am using wifi connection.
Please use plain text.
Developer
peter_strange
Posts: 19,506
Registered: ‎07-14-2008

Re: HttpPost- not getting response

"I am getting response code 0"

 

I don't see you checking the respnse code in your code.  I suggest that you that.

 

Can you answer the other questions I asked to.  And here is a few more. 

 

How do you know that you have even performaned a request - if you don't have data coverage you will get null. 

Have you stepped through your code making sure it is beign executed.  What statement do you get too?

Please use plain text.
Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Carrier: Vodafone

Re: HttpPost- not getting response

[ Edited ]

i Have checked response code and response message before. Sorry i have not specified in above code. I am not getting any exception. I get null value in string. Actually i am running the code on simulator. When i debug the code, debugging stops at "int len = (int)httpConnection.getLength();" and shows something like this "DatagramProtocol(ConnectionBase).receive(Datagram) line: 538 "
Parameters are getting append properly in postdata.

Please use plain text.
Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Carrier: Vodafone

Re: HttpPost- not getting response

It was matter of "What connection method are you using". Thanx for the article. I am getting response properly now.
Please use plain text.
Developer
peter_strange
Posts: 19,506
Registered: ‎07-14-2008

Re: HttpPost- not getting response

Be aware that this all changes when you put your app on device.  On device, you will have to:

 

a) Code some checking to determine which connection method you can use,

 

or

 

b) Change to using ConnectionFactory (assumoing OS 5.0 and above).

 

Can I recommend you try ConnectionFactory. 

Please use plain text.
Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Carrier: Vodafone

Re: HttpPost- not getting response

thanks. I have written code to determine connection.
Please use plain text.