J2ME RMS, HttpConnection

Basically a blog which is a mixture of anything that comes up in the author's mind. From the technologies that makes his life convenient to the everyday news in his country.

J2ME RMS, HttpConnection

Monday, March 10, 2008

(0) Comments

Last week we finished a big part of our J2ME project which is a commercial application that will cater Malls for their mobile inventory of items using low end phones. We are successful in integrating my Mobile client with the host server. What we did was post data from mobile to host, however this morning I found a strange problem my alert that will tell me if my post works fine did not alert! So I was busy debugging many of my codes try implementing several threads unluckily I was unable to solve the problem. Well this lunch I've noticed a little line space and I was so sure that it was a newline fortunately it was just a host misq for there was a newline inserted in my expected response unfortunately it took my 2 hours :D. Part of the codes:
[code]
Thread deadLock = new Thread(new Runnable() {
String strReply = "";
public void run() {
WaitCanvas cWait = new WaitCanvas();
HttpConnection c = null;
InputStream is = null;
midlet.SetDisplay(cWait);
try {
int rc;
c = (HttpConnection)Connector.open(Settings.Post_Url + strParams);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT");
c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
Alert a = new Alert("Error", "Connection to host failed.", null, AlertType.ERROR);
a.setTimeout(Alert.FOREVER);
a.addCommand(cmdBack);
a.setCommandListener(new MyCommandListener());
midlet.SetDisplay(a);
}
is = c.openInputStream();
int len = (int)c.getLength();
if (len > 0) {
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;
}                 
strReply = new String(data);
}        
} catch(Exception e) {
//do something here
} finally {
try {
if (is != null)
is.close();
if (c != null)
c.close();
} catch(Exception e) { }
}
if(strReply.equals("1")) {
//do something here
} else if(strReply.equals("0")){
//do something here
} else if(strReply.equals("2")){
//do something here
}
}
});
deadLock.start();
[/code]

0 Responses to "J2ME RMS, HttpConnection"

Post a Comment

Contributors