problems pulling data from a url..

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

problems pulling data from a url..

sergio_101-2
i am currently using HTTPSocket to pull data from a url...

looks like this:

        | data |
        data := String
                                fromByteArray: (HTTPSocket httpGet: anUrl).

i end up with a string that has my desired data (a JSON string), but,
it's preceeded by this:

'a RWBinaryOrTextStream ' '<here is the stuff i need>' ' '

i am trying to lop off the first part by doing:

jsonData := dataString replaceAll:  'RWBinaryOrTextStream' with: 'test'.

just to see if i can touch the string..

but no matter what i do, i can't get any parts of the string to be raplaced..

it almost looks like there are three strings in there..

the RWBinaryOrTextStream one, the one i need, and an empty one..

is there a better way to pull a url?

thanks!


--

----
peace,
sergio
photographer, journalist, visionary

http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: problems pulling data from a url..

cedreek
Hi Sergio,

2010/3/24 sergio_101 <[hidden email]>
i am currently using HTTPSocket to pull data from a url...

looks like this:

       | data |
       data := String
                               fromByteArray: (HTTPSocket httpGet: anUrl).

the problem here is that HTTPSocket httpGet: '' return a RWBinaryOrTextStream.

To get the string, you need to send the message contents to the stream.

(HTTPSocket httpGet: 'json url') contents

 

i end up with a string that has my desired data (a JSON string), but,
it's preceeded by this:

yes because it's a stream, not a string.

hth, 

Cédrick
 

'a RWBinaryOrTextStream ' '<here is the stuff i need>' ' '

i am trying to lop off the first part by doing:

jsonData := dataString replaceAll:  'RWBinaryOrTextStream' with: 'test'.

just to see if i can touch the string..

but no matter what i do, i can't get any parts of the string to be raplaced..

it almost looks like there are three strings in there..

the RWBinaryOrTextStream one, the one i need, and an empty one..

is there a better way to pull a url?

thanks!


--

----
peace,
sergio
photographer, journalist, visionary

http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



--
Cédrick

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: problems pulling data from a url..

David Mitchell-10
In reply to this post by sergio_101-2
(HTTPSocket httpGet: aUrl) contents

I know we Smalltalkers like to talk about how no one ever gets confused about dynamic types, but it does happen ;-).

The stream has the text you want. May want to familiarize yourself with core methods on the Stream hierarchy.

Pushing the Stream through a ByteArray conversion is invoking printString.

Your trouble with getting a modified string using replaceAll: is again a subtle typing error:

Reading the comment for #replaceAll:with:
"Replace all occurences of oldObject with newObject"

But, Strings are collections of characters, not strings. Compare:

'foo' replaceAll: 'f' with: 'g'
'foo' replaceAll:  $f with: $g 

Take a look at senders of #findString:

Many of them do replacements of substrings.

David Mitchell
http://www.withaguide.com

On Wed, Mar 24, 2010 at 12:44 AM, sergio_101 <[hidden email]> wrote:
i am currently using HTTPSocket to pull data from a url...

looks like this:

       | data |
       data := String
                               fromByteArray: (HTTPSocket httpGet: anUrl).

i end up with a string that has my desired data (a JSON string), but,
it's preceeded by this:

'a RWBinaryOrTextStream ' '<here is the stuff i need>' ' '

i am trying to lop off the first part by doing:

jsonData := dataString replaceAll:  'RWBinaryOrTextStream' with: 'test'.

just to see if i can touch the string..

but no matter what i do, i can't get any parts of the string to be raplaced..

it almost looks like there are three strings in there..

the RWBinaryOrTextStream one, the one i need, and an empty one..

is there a better way to pull a url?

thanks!


--

----
peace,
sergio
photographer, journalist, visionary

http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: problems pulling data from a url..

sergio_101-2
In reply to this post by cedreek
> the problem here is that HTTPSocket httpGet: '' return
> a RWBinaryOrTextStream.
> To get the string, you need to send the message contents to the stream.
> (HTTPSocket httpGet: 'json url') contents
>

thanks!

just getting back to this..

my question is.. how do i know what part of the stream to look at? as
in, how would i know that contents is the part i am interested in?

thanks!

oh.. and i am reading the docs about streams again...

thanks again!


--

----
peace,
sergio
photographer, journalist, visionary

http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: problems pulling data from a url..

cedreek

> the problem here is that HTTPSocket httpGet: '' return
> a RWBinaryOrTextStream.
> To get the string, you need to send the message contents to the stream.
> (HTTPSocket httpGet: 'json url') contents
>

thanks!

just getting back to this..

my question is.. how do i know what part of the stream to look at? as
in, how would i know that contents is the part i am interested in?

thanks!

oh.. and i am reading the docs about streams again...

thanks again!



Not sure I understand... :)

A stream encapsulates a collection and provide facilities to navigate forward, backward, etc... (strings are collection - direct string manipulation aren't efficient)...  So you use a stream to manipulate efficiently your string, and to get the string back (or whatever collection that was associated to the string), you send the message #contents.

If you se the class comment of Stream 
"I am an abstract class that represents an accessor for a sequence of objects. This sequence is referred to as my "contents"."


hth, 



 
--

----
peace,
sergio
photographer, journalist, visionary

http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



--
Cédrick

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners