Skip to main content

HttpConnection

4 replies [Last post]
gerrit_jvv
Offline
Joined: 2005-09-26

I was using the below code on most of the nokia phones when latelly my company decided to test our product with the new nokia n93
something very interesting occurred and that is that java write once run everywhere is not always as simple and less when things like
wat happenned are not documented any where......

The following code failed on the Nokia N93

String str = "HelloWorld";
byte[] bts = str.getBytes();

javax.microedition.io.HttpConnection con = (javax.microedition.io.HttpConnection)Connector.open(_URL, Connector.READ_WRITE, true);
con.setRequestMethod(javax.microedition.io.HttpConnection.POST);

//-- this is the source of the problem
con.setRequestProperty("IF-Modified-Since", "10 Nov 2000 17:29:12 GMT");
con.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
con.setRequestProperty("Content-Language", "en-CA");
con.setRequestProperty("Content-Type", contentType);

con.setRequestProperty("Content-Length", String.valueOf(bts.length));

OutputStream os = con.openOutputStream();

os.write(bts);
//---- Here the code fails i.e. the phone takes about 1 minute to react and throws an IOException: -33
os.close();

It took me 3 days sweating and testing to find out why? try google? nothing, try java.sun.com? nothing, try nokia developer website? nothing, try symbianos website? nothing.

I found out that the IF-Modified-Since value must be that of the current date and time at which the connection is openened or else the java running on nokia will elegantly through an IOException: -33 exception, this helps alot and saves you alot of time, because as you can see IOException: -33 tells you everything you need to know. I would like to congratulate the person who wrote this code and stab him a view times to show my gratitude.

To fix it I put in
java.util.Date d = new java.util.Date(System.currentTimeMillis());

con.setRequestProperty("IF-Modified-Since", d.toString());

Anybody doing connections out there and whant to use the nokia n93 although I suspect this may be something that will greep up with any new nokia, if you don't include the current date and time in the IF-Modified-Since property your code will fail and wont even write out to the output stream.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
terrencebarr
Offline
Joined: 2004-03-04

Gerrit,

Thanks much for this heads-up! I've added a link in the Application Developer TWiki "Device Issues and Gotchas" section: http://wiki.java.net/bin/view/Mobileandembedded/DeviceIssues

Feel free to update that section as you find useful in the future.

-- Terrence

Terrence Barr
Evangelist, Java Mobile & Embedded Community
Sun Microsystems, Germany
www.mobileandembedded.org

gerrit_jvv
Offline
Joined: 2005-09-26

Thanks Terrence,
I work alot with JME on alot of different devices and will post any gotchas and issues I find along the way too http://wiki.java.net/bin/view/Mobileandembedded/DeviceIssues
in the future.

Regards,
Gerrit

Chris B

I run a separate thread that handles storing http data, but it pauses all
other threads when it stores to the RecordStore. Is there anyway around
this?

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

gerrit_jvv
Offline
Joined: 2005-09-26

Storing http data?

What kind of data? data read from an Http stream and stored as String, bytes[]?

Which threads do you have running? 2 threads should not normally pause or freeze each other.

I need some more information to say something usefull, but if it will help:
If the reason you are saving data read, to the record storage is to save memory consumption, then its from my experience better to put the READ FROM HTTP code and the WRITE TO RECORD STORAGE in the same thread.

Something like (note this is only an indication and needs more code to be functional):

class MyReader extends Thread{

public void run(){

byte[] buff = new byte[buffSize];
InputStream in = gethttpConnectionInput();

while(fillBuff(buff, conn) > 0)
writeBuffToStorage(buff);

closeConnection(in);

}
}

Having one thread read all the Http code and another different thread writting to the record store will at some stage and depending on the data read, lead to an OutOfMemory ERROR. This is because one thread may read faster than the writing thread.

Also because mobile processors are very very slow, try displaying a Progress bar when reading/writting http connections or record storage. In doing so the user will not notice any threads slowing down, because there's not anough processing power.