Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Odd Problem - HttpConnection functionality

 
Frank Manno
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Michael Yuan
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Frank Manno
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I do:
 
David Price
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i totally agree with David.
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic