stream and string array

F

focode

i have opened an HttpConnection to my url (www.schoolssmart.in/mdm/
abc.php" then oped a input stream on this connection , read the data
in byte[] data = new byte[length];
in.read(data);
then converted this into string by
String response = new String(data);
and then displays it ..
the content of the response is arunesh class1 office ...etc
i want to collect this string individually in string array such that
str[1] = arunesh
str[2]=class1
str[3]=office and so on ..

i can do this by reading the space and putting some logic to separate
individual string ...Is their any stander ed way to do this in java ?
 
S

Simon

i have opened an HttpConnection to my url (www.schoolssmart.in/mdm/

Does not work for me.
then oped a input stream on this connection , read the data
in byte[] data = new byte[length];
in.read(data);

Are you aware that InputStream.read(byte[]) does not necessarily read
the entire buffer?
then converted this into string by
String response = new String(data);

Since you are taking this from a HTTP connection you probably want to
specify a Charset explicitly:

new String(data, someCharset)

You can as well wrap a Reader around your InputStream (with the
appropriate encoding). In that case you don't have to handle the
buffering etc. yourself. BufferedReader has a readLine method which
seems appropriate for your application.
and then displays it ..
the content of the response is arunesh class1 office ...etc
i want to collect this string individually in string array such that
str[1] = arunesh
str[2]=class1
str[3]=office and so on ..

If you want to split at whitespace, you can use String.split(regexp);
You can also look at

http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html

or

http://java.sun.com/javase/6/docs/api/java/io/StreamTokenizer.html

if this is more appropriate.

Cheers,
Simon
 
F

focode

i have opened an HttpConnection to my url (www.schoolssmart.in/mdm/
abc.php"

Does not work for me.
then oped a input stream on this connection , read the data
in byte[] data = new byte[length];
in.read(data);

Are you aware that InputStream.read(byte[]) does not necessarily read
the entire buffer?
then converted this into string by
String response = new String(data);

Since you are taking this from a HTTP connection you probably want to
specify a Charset explicitly:

   new String(data, someCharset)

You can as well wrap a Reader around your InputStream (with the
appropriate encoding). In that case you don't have to handle the
buffering etc. yourself. BufferedReader has a readLine method which
seems appropriate for your application.
and then displays it ..
the content of the response is arunesh class1 office ...etc
i want to collect this string individually in string array such that
str[1] = arunesh
str[2]=class1
str[3]=office and so on ..

If you want to split at whitespace, you can use String.split(regexp);
You can also look at

http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html

or

http://java.sun.com/javase/6/docs/api/java/io/StreamTokenizer.html

if this is more appropriate.

Cheers,
Simon

Dear Simon
i appreciate your help.
the url is "http://schoolsmart.in/mdm/abc.txt" their are
strings i can read them well , so no buffer problem , but i am working
on j2me platform and it doesn't support StringTokenizer nor
StreamTokenizer (all packages are not available fron j2se ) so if any
another way .....!
 
S

Simon

the url is "http://schoolsmart.in/mdm/abc.txt" their are
strings i can read them well , so no buffer problem , but i am working
on j2me platform and it doesn't support StringTokenizer nor
StreamTokenizer (all packages are not available fron j2se ) so if any
another way .....!

And what about the first option, String.split()? Of course you have to
be careful because you cannot easily escape whitespace with this mechanism.

Cheers,
Simon
 
F

focode

And what about the first option, String.split()? Of course you have to
be careful because you cannot easily escape whitespace with this mechanism.

Cheers,
Simon

no dear String.split()? also not works i will have to find another way
out may be write a method for it ....!!!!!!
 
K

Knute Johnson

Simon said:
Since you are taking this from a HTTP connection you probably want to
specify a Charset explicitly:

new String(data, someCharset)

Isn't the charset specified in the server response? If so, selecting a
charset other than the one used by the server would cause him problems I
should think.
 
S

Simon

Since you are taking this from a HTTP connection you probably want to
Isn't the charset specified in the server response? If so, selecting a
charset other than the one used by the server would cause him problems I
should think.

Exactly, but since he is reading only bytes, new String(byteBuffer) will
not know which charset the server specified, will it? Or am I missing
something?

To be more clearly, I should not have said "a Charset", but rather the
one returned by connection.getContentEncoding().

Cheers,
Simon
 
K

Knute Johnson

Simon said:
Exactly, but since he is reading only bytes, new String(byteBuffer) will
not know which charset the server specified, will it? Or am I missing
something?

To be more clearly, I should not have said "a Charset", but rather the
one returned by connection.getContentEncoding().

I haven't played with URLConnection much but I believe
getContentEncoding() will return the charset used by the server. So yes
I agree that he needs to specify the charset from the connection in the
String constructor.
 
K

Knute Johnson

S

Simon

No, getContentEncoding() returns things like gzip or compress.
getContentType() returns the MIME type. If text/*, it might have a
charset parameter on the end of it, e.g. text/html; charset=ISO-8859-1.

Oops, thank you for correcting this. Shame on me.

Cheers,
Simon
 
R

Roedy Green

No, getContentEncoding() returns things like gzip or compress.

getContentType() returns the MIME type. If text/*, it might have a
charset parameter on the end of it, e.g. text/html; charset=ISO-8859-1.

see http://mindprod.com/products1.html#HTTP

its GET class lets you specify your preferred encoding, but decodes
the response based on the actual encoding returned by the server in
the header.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"We must be very careful when we give advice to younger people: sometimes
they follow it!"
~ Edsger Wybe Dijkstra, born: 1930-05-11 died: 2002-08-06 at age: 72
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top