Random thoughts shooting out of volatile mind
Posting Data via Http from Blackberry
In this post i'm going to tel how to post the data to web services, don't misunderstand web service to SOAP services. I'm talking about normal webpages designed using jsp,php or asp or similar server side technology. We will be using URLEncodedPostData class fromnet.rim.blackberry.api.browser package to create a url encoded data which will be posted to webserver. For connecting to a particular website we will use HttpConnection class defined in javax.microedition.io package.
First let us see how to create URL encoded data to post to server using URLEncodedPostData class. I should remind you this is a signed class and requires code signing for loading the program into actual device. If you want to avoid this you need to write your own function to encode your data into URL Encoded format. Now lets see how to do this using URLEncodedPostData class
URLEncodedPostData _postData = new URLEncodedPostData("",false);
_postData.append("name","value");
First argument to constructor is encoding which is to be used leaving it as empty string means use default encoding. Second argument tells whether to use WAP conventions for encoding or not. append(String,String) function is used to create the URL encoded data. For example if your data is in the form fname=vasudev&lname=kamath then you can create this string by writing like this
_postData.append("fname","vasudev");
_postData.append("lname","kamath");
Now you know how to prepare your data lets move forward to creating connection to the webserver and then sending the prepared data using HTTP POST method. We will be using HttpConnection class for this purpose.
bytes [] postDataBytes = _postData.getBytes();
HttpConnection _httpCon = (HttpConnection)Connector.open(url);
_httpConnection.setRequestMethod(HttpConnection.POST);
_httpConnection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
_httpConnection.setRequestProperty("Content-Language", "en-US");
_httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
_httpConnection.setRequestProperty("Content-Length",(new Integer(postDataBytes.length)).toString());
setRequestMethod function tells which method is used to send data it can have 2 values either HttpConnection.GET or HttpConnection.POST. The "User-Agent" string tells webserver about the browser which is sending the data. All above mentioned properties are present in standard Http headers and more detail on them can be found in this link. Of all the properties important ones are last 2 i.e Content-Type and Content-Length if you forget to mention these to webserver will not be able to read the data you sent. Here Content-Type is set to "application/x-www-form-urlencoded" which tells webserver that request contains url encoded form data. If you are planning to send files in the request to browser you should set Content-Type to "multipart/form-data" instead of this if you use  "application/x-www-form-urlencoded" you will get response code as 500 meaning internal server error. Now you have prepared HTTP header with all required fields next step is to write the data to webserver which is done as follows.
OutputStream os = _httpCon.openOutputStream();
os.write(_postData.getBytes());
os.flush(); // not required
int rc = _httpCon.getResponseCode(); //causes data to be sent to server and get response back
Depending on the response code you can do further processing. Response code for successful post will be 200 (HttpConnection.HTTP_OK) for more on response codes please see this link.
Here is a generic class which i designed for posting the data and getting response back in the form of string. Hope it will help you in your program. If there are  any mistakes in my post or in the code or any improvement to the code please mention it as comment.
import java.io.*;
import javax.microedition.io.*;
import net.rim.blackberry.api.browser.URLEncodedPostData;
class HttpPost
{
private String _url;
private URLEncodedPostData _postData;
private byte[] _data;
private HttpConnection _httpConnection;
private OutputStream os;
private InputStream is;

HttpPost(String url,URLEncodedPostData data) {
_postData = postData;
_url = url;
try {
_httpConnection = (HttpConnection)Connector.open(_url);
} catch(Exception e) {
}
}

public String postData() {
try {
bytes [] postDataBytes = _postData.getBytes();
_httpConnection.setRequestMethod(HttpConnection.POST);
_httpConnection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
_httpConnection.setRequestProperty("Content-Language", "en-US");
_httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
_httpConnection.setRequestProperty("Content-Length",new Integer(postDataBytes.length).toString());
os = _httpConnection.openOutputStream();
os.write(_postDataBytes);

int rc = _httpConnection.getResponseCode();
if(rc == HttpConnection.HTTP_OK) {
is = _httpConnection.openInputStream();
is.read(_data);
} else {
_data = null;
}
} catch(Exception e) {
}
return (new String(_data));
}
}
In my next post i'll show how XML files can be handled in blackberry using KXML library. Till then C'ya :)
(Note: In case you are not able to see the code properly because of template you can refer to my other blog here)
Posted by: copyninja on Sunday, 13 September 2009

blog comments powered by Disqus
Fork me on GitHub