Click to See Complete Forum and Search --> : BufferedReader problem


stanley007
April 24th, 2003, 06:36 AM
Is there any method other than readline to use with bufferedreader to get the text stored ???


public String HTTPPOST(String strURL, String strArg)
{
try
{

// URL of CGI-Bin script.
URL url = new URL(strURL);

URLConnection con = url.openConnection();

con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);

// Specify the content type.
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

// Send POST output.
OutputStream os = con.getOutputStream();

OutputStreamWriter osw = new OutputStreamWriter(os);

osw.write(strArg);
osw.flush ();
osw.close ();

// Get response data.
InputStream is = con.getInputStream();

// any response?
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = "";

// THIS IS WHERE THE PROBLEM IS, I CAN'T GET THIS BUFFEREDREADER TO RETURN THE STRING THAT THE WEBSERVER RETURNS AS IT IS ONE LINE OF TEXT WITH NO LINE BREAKS OR CARRIAGE RETURNS

while(br.read() != -1) line = line + br.read(); // THIS JUST RETURNS A LIST OF INTs

while(br.readLine() != null) line = line + br.readLine(); // THIS JUST RETURNS A null AS THE DATA BEING RETURNED BY THE WEBSERVER IS ONE LINE OF TEXT WITH NO LINE BREAKS OR CARRIAGE RETURNS - THIS WORKS FINE WITH EVERY OTHER PAGE I'VE TRIED.


return(line);

}
catch (MalformedURLException me)
{
return("MalformedURLException: " + me);
}
catch (IOException ioe)
{
return("IOException: " + ioe.getMessage());
}
}

Ishaibin
April 24th, 2003, 09:45 AM
while(br.read() != -1) line = line + br.read();

while(br.readLine() != null) line = line + br.readLine();


i think this is the problem , because first you read using the read method - which reads bytes if im not mistaken and not chars nor string .
and then , after all the data has been read you try to read a full line - but there is simply no data to read any more !
so it returns a null

simply delete the first line :

while(br.read() != -1) line = line + br.read();


i hope this is it .

stanley007
April 24th, 2003, 10:24 AM
Thanks Ishaibin for your help, BUT I am using either of the two line and not both at one go.

The problem with br.read() is it returns a list of integers

br.readLine() doesn't work as THE DATA BEING RETURNED BY THE WEBSERVER IS ONE LINE OF TEXT WITH NO LINE BREAKS OR CARRIAGE RETURNS .

So i am stuck as I don't know how to read the string stored in br
is there any other method or any other way to do it .

Any help will be greatfull.:confused:

viravan
April 24th, 2003, 11:14 AM
Why don't you try the following:


while(br.read() != -1) line = line + (char)br.read(); // THIS JUST RETURNS A LIST OF INTs


In other words, cast the INT to a char!

:)

V.V.

Ishaibin
April 24th, 2003, 07:27 PM
simply add a token for where the line ends , maybe use the token
" . " (dot) .
then you recieve a big line and all you need to do is to split it to smaller lines using the token .


AND !
if you want to make your life much easier , simply cancle the buffer and recieve the strings as is .
the buffer simply waits until he gets enough strings and then returns them to you .
and because those are just strings , not using the buffer will not make the perfomances any worse .
after you will cancle the buffer you will get one string at a time.

viravan
April 24th, 2003, 08:28 PM
Originally posted by Ishaibin
simply add a token for where the line ends , maybe use the token
" . " (dot) .
then you recieve a big line and all you need to do is to split it to smaller lines using the token .


AND !
if you want to make your life much easier , simply cancle the buffer and recieve the strings as is .
the buffer simply waits until he gets enough strings and then returns them to you .
and because those are just strings , not using the buffer will not make the perfomances any worse .
after you will cancle the buffer you will get one string at a time.


OR... you can cast it to a char.....see example from Sun at the link shown below:

http://java.sun.com/docs/books/j2mewireless/examples/src/examples/addressbook/NetworkQuery.java

:)

V.V.

stanley007
April 25th, 2003, 04:43 AM
Thanks to all of you for the help. The code seems to be working now .

cheers

Ishaibin
April 25th, 2003, 07:41 AM
what did you do in the end ?