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

HttpConnection

 
Greg Schwartz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has anyone experienced strange behavior while using HttpConnection to connect to networks with J2ME? We think our problem may have something to do with flushing the output stream.
We have been testing an app that makes an HttpConnection to a PHP page. We send one GET paramater in the URL and then a string of data using the POST method as follows:
*************************************8
String url = "http://www.webaddress.com/emailServer.php?emailAddr="+emailAddr;
hc = (HttpConnection)Connector.open(url);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("Content Type", "application/x-www-form-urlencoded");
hc.setRequestProperty("Content-Length", Integer.toString(message.length()));
hc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0" );
hc.setRequestProperty("Content-Language", "en-US" );
out = new DataOutputStream(hc.openOutputStream());

for(int i=0; i <messageBody.length; i++)
{
out.writeByte(messageBody[i]);
eGauge.setValue(i);
}

out.flush();
out.close();
/*the try, catch, throw code was left out from this sample*/
*************************
The application will work successfully several times but then begin failing indefinately. We noticed that the application is failing when flushing the output stream. It seems to delay for a while and then throws an error.
If anyone has any idea what could be causing such erratic behavior, I'd appreciate your help. Thanks so much!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly, In Jonathan Knudsen's book, he highly suggests using the Get method instead of the Post method, as it is easier, and don't have to worry about the Header.
Just add to the URL the values that you want to pass back to the Servlet.
Not that that is a direct answer to your question, but I know that there are others here that have more experience than my one midlet and reading of a book.
Well I did learn some great things at Michael Yuan's BoF at JavaOne.
Mark
 
Greg Schwartz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
I appreciate your reply. The only reason I stayed away from using the GET method for everything was because the String I would have to append to the URL was extremely long (a few pages long).
Do you know if there is any restriction on how long the variables can be when using the GET method?
Thanks,
Greg
 
Michael Bronshteyn
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had issues using Sun's implementation of HttpConnection. The only thing, I was using J2SE and not J2ME. Check this thread out. Maybe you can find some helpful info there.thread
Changing HttpConnection to use jakarta-commons solved the problem I mentioned in that thread.
[ June 18, 2003: Message edited by: Michael Bronshteyn ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg, that is a good question. I thought I had read somewhere that it was a certain size, but I can't remember it, so I don't want to give you bad information. Since the question is really about the URL encoding, then I'd suggest looking at the J2EE Specification, basically on Servlets, and see if the information is in there.
Or better yet, someone else on this site might know the exact answer already.
Mark
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At least some older Nokias had a restriction of max 250 or so characters for a URL in the WML/WAP browser. Maybe that restriction was due to the underlying protocol stack implementation... Anyway, I don't think there is such restrictions on newer devices. The HTTP protocol doesn't define any such restriction.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic