| Reply | « Previous Thread | Next Thread » |
|
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 |
|
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 :) |
|
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);
}
Cheers, Graham. |
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
|
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 | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|