This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes HttpConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Mobile » Java Micro Edition
Reply Bookmark "HttpConnection" Watch "HttpConnection" New topic
Author

HttpConnection

Greg Schwartz
Ranch Hand

Joined: May 11, 2003
Posts: 132
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

Joined: Feb 05, 2001
Posts: 17243
    
    1

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Greg Schwartz
Ranch Hand

Joined: May 11, 2003
Posts: 132
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

Joined: Mar 26, 2002
Posts: 85
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 ]

Michael
SCJP2
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17243
    
    1

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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: HttpConnection
 
Similar Threads
content-length property in wireless toolkit
HTTP Client/Server Problem
HTTPS / MIDP 1.0
Error Initializing http tunnel connection.
checking for internet connectivity