Projects and Talks
URLEncodedPostData _postData = new URLEncodedPostData("",false);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("name","value");
_postData.append("fname","vasudev");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.
_postData.append("lname","kamath");
bytes [] postDataBytes = _postData.getBytes();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.
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());
OutputStream os = _httpCon.openOutputStream();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.
os.write(_postData.getBytes());
os.flush(); // not required
int rc = _httpCon.getResponseCode(); //causes data to be sent to server and get response back
import java.io.*;In my next post i'll show how XML files can be handled in blackberry using KXML library. Till then C'ya :)
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));
}
}