You Are Here:

Community: Developer Discussion Boards

#1 Old Can't get data from http connection - 2003-10-11, 03:04

Join Date: Aug 2003
Posts: 21
rebeljack
Offline
Registered User
Hello

I made an application that establishes a http connection to get some data. It works fine on the emulators with localhost, but I can't make it work on a 3650!
The code I use is:

HttpConnection hc = null;
InputStream in = null;
int contentLength;
byte[] raw;
int length;

try {
hc = (HttpConnection) Connector.open(url);
in = hc.openInputStream();

contentLength = (int) hc.getLength();

raw = new byte[contentLength];
length = in.read(raw);

in.close();
hc.close();
text = new String(raw, 0, length);
}
}
catch (IOException ioe) {
System.out.println(ioe.toString());
}

contentLegth is always -1. Is something wrong with this code? I think I made this based on some example...

thanks for any help
Jack
Reply With Quote

#2 Old 2003-10-11, 11:52

Join Date: Mar 2003
Posts: 112
ossipetz
Offline
me-2-j :)
http is request-response protocol

so i would suggest you fist to a request and then get you data.

try to have a look at http://wireless.java.sun.com/midp/articles/network/

which shows it in detail.
it will work then :-) (hopefully)

good luck :)
Reply With Quote

#3 Old 2003-10-11, 15:47

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
getLength() returns -1 if there is no content-length field in the HTTP response. This would be unusual... HTTP servers usually transmit this information, but perhaps it is being stolen by your WAP gateway(??) (I blame WAP gateways for all network-related problems!) Unless you're not using a WAP connection (do you need to on a 3650?), in which case the blame lies elsewhere...!

If your data-source is a script with unbuffered output, you may need to generate your own content-length header field.

You could try transmitting your own length-information in the response body, or using some kind of end-of-file marker at the end of your transmission.

Or open a DataInputStream and use readFully().

Or, perform read()s in a loop until you get no more data; something like:
Code:
byte [] buffer = new byte [BUFFER_SIZE];
int nBytesRead;
while ( (nBytesRead = in.read (buffer)) > 0 ) {
    // process this block, for example:
    String s = new String (buffer, 0, nBytesRead);
    myStringBuffer.append (s);
}
Best of luck! Would you post back here the solution that you find works best?

Cheers,
Graham.
Reply With Quote

#4 Old 2003-10-12, 05:13

Join Date: Aug 2003
Posts: 21
rebeljack
Offline
Registered User
Hi

Thank you for your excelent help!

I follow the examples that were in the page Ossipetz indicated. Basically the change I made was the read loop you suggested...and it worked fine!

Jack
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia