GeeCON Prague 2014*
The moose likes Java Micro Edition and the fly likes Odd Problem - HttpConnection functionality Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Odd Problem - HttpConnection functionality" Watch "Odd Problem - HttpConnection functionality" New topic
Author

Odd Problem - HttpConnection functionality

Frank Manno
Ranch Hand

Joined: Apr 06, 2003
Posts: 48
Hey all,
I've been working all day trying to figure this out. I'm working on a MIDlet that connects to a server, executes a CGI script, which connects to an Oracle database and returns a result based on a value entered into a textfield.
I'm basing the code off of Jonathan Knudsen's "Jargoneer.java" code (from his 2nd edition book). The odd part is that his code works without a hitch. The code I'm using, which is almost identical apart from variable names, fails.

The odd part is that if I use a StringBuffer to read the contents of the query's resultset, and then convert the Buffer into a String, all works fine:

Is there a reason why my code isn't working above?! Any help would be greatly appreciated!

Thanks!


-- Frank
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
I think you have to read the input stream byte by byte. That "read()" method you had only reads in the next byte from the stream.


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
Frank Manno
Ranch Hand

Joined: Apr 06, 2003
Posts: 48
Michael,
Thanks for responding... What you say makes sense, and that's why I ended up trying it with the StringBuffer, and looping each time to read byte by byte. What I find so boggling is that Jonathan's method works perfect in his example:

That's the code right out of his Jargoneer MIDlet. Is there something I'm missing in my code that he has? It just doesn't make sense that his works and mine doesn't.
I appreciate any suggestions you may have!

Thanks again!
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Maybe the difference is at the server side. Does Jonathan provide server code? Can you deploy and test the entire end-to-end setup on your computer?
Frank Manno
Ranch Hand

Joined: Apr 06, 2003
Posts: 48
Michael,
Unfortunately, he doesn't... I assume because it's out of scope of the example/book.
That's what I was thinking as well, is that there may be something going on at the Servlet level. So that's a possibility?
Also, when I was searching through the forums for any already-existing problems, I ran across this thread (http://www.coderanch.com/t/226399/JME/Mobile/Wireless-connection-speed-ME-app). There's mention that the method Jonathan uses will only work _IF_ the server supports the "content-length" header. Can you confirm whether this is true?

Thanks again... I really appreciate it!
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Well, if content-length is not set, you code automatically allocates a 255 byte array and try to read in the data in that one chunk. But if the server response is really longer than 255 bytes, you program only reads part of it. So, yes, that is likely to cause application errors later.
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
This is what I do:
David Price
Ranch Hand

Joined: Jan 22, 2003
Posts: 93
One thing that may be relevant is that Frank's method will fail if the data doesn't all come in one lump (e.g. TCP packet). That method read(byte[]) isn't guaranteed to read the whole content of the InputStream, just whatever arrives in the next lump. That's why Michael's method is better, as he keeps calling the method repeatedly until it returns -1. That would also explain why the StringBuffer approach works.
Jonathan's code may work because his server reliably sends all the data in one lump (carefully chosen technical term ). Maybe Frank's server writes the data to the TCP connection in several lumps (e.g. several TCP packets). I use code similar to Michael's when I care about performance, and code similar to the StringBuffer approach when I know the data is 1-byte characters and just want to keep things simple.
dan moore
Ranch Hand

Joined: Nov 04, 2001
Posts: 68
i totally agree with David.


dan moore, infomatiq ltd.<br />email dan@infomatiq.co.uk<br /><a href="http://www.infomatiq.co.uk" target="_blank" rel="nofollow">http://www.infomatiq.co.uk</a>
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
Originally posted by Michael Yuan:
This is what I do:


This is the proper way to read from the stream - byte by byte is terribly slow.
Frank Manno
Ranch Hand

Joined: Apr 06, 2003
Posts: 48
Hey all,
Sorry for not responding sooner... I ended up going with a StringBuffer and just appended each character as I read it in. I'm not sure how efficient this is, but I'd appreciate any feedback on that issue.
I'd like to thank everybody who pitched in their help, including Jonathan Knudsen who also helped out!
I ended up finding out that the Apache server was sending the data back in 3 chunks. My professor was the one who found out, unfortunately, after I had already found a workaround.
It certainly provided me with some insight I would never have looked into had I not run into this problem.
Thanks again!
Frank
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Odd Problem - HttpConnection functionality