 |
 |
|
 |
04-11-2008, 07:40 PM
|
#1 (permalink)
|
|
Guest
|
Pulling Data From A Web Page
Hi Everyone,
I'm trying to use the CFHTTPRequest library to pull data from a web page. All the web page has is a pipe deliminated string of some information about our server status. I want to pull this into the iPhone and display it in a pretty gui interface.
My issue is that i can't figure out how to get the body of the web page. I've looked through a few examples and I keep getting lost. Below is the code i'm using, please give me some idea on how i would get the body, and then throw it into a string where i can then put the data into variables within a class.
Code:
CFHTTPMessageRef request;
CFReadStreamRef stream;
//init: url is a string like "www.someplace.com"
NSString *theURL = [NSString stringWithFormat:@"http://%@", url];
NSURL *urlObj = [NSURL URLWithString:theURL];
request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"), (CFURLRef)urlObj, kCFHTTPVersion1_0);
//fetch
stream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault,request);
CFReadStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
CFHTTPMessageRef response = (CFHTTPMessageRef) CFReadStreamCopyProperty(stream, kCFStreamPropertyHTTPResponseHeader);
//not sure if this is correct :(
CFDataRef *body = CFHTTPMessageCopyBody(response);
//now what?
Am I heading in the right direction? Or is there a better way to do this?
If I am doing it right, how do I turn body into a string of actual text that I can manipulate?
Thanks,
CoffeeKid
|
|
|
|
04-11-2008, 10:02 PM
|
#2 (permalink)
|
|
Guest
|
Re: Pulling Data From A Web Page
I figured it out. For anyone that also needs to do this, its simplier then it looks.
Here's the Code:
Code:
NSString *urlStr = [NSString stringWithFormat:@"%@/index.php?some_name=%@", url, name];
NSURL *theURL = [[NSURL alloc] initWithString:urlStr];
//get the data from the web page
NSString * results = [NSString stringWithContentsOfURL:theURL];
//Seperate the data by a | delimiter
NSArray * chunks = [results componentsSeparatedByString:@"|"];
//put the data into some class variables
NSScanner * scanner = [[NSScanner alloc] initWithString:[chunks objectAtIndex:0]];
[scanner scanFloat:&load];
scanner = [[NSScanner alloc] initWithString:[chunks objectAtIndex:3]];
[scanner scanInt:&diskUsage];
users = [[NSString alloc] initWithString:[chunks objectAtIndex:4]];
Hope this helps someone, because this took me HOURS to figure out!
-CoffeeKid
|
|
|
|
04-12-2008, 02:13 AM
|
#3 (permalink)
|
|
Junior Member
Join Date: Apr 2008
Posts: 7
Rep Power: 0
|
Re: Pulling Data From A Web Page
Thanks for that. I can't believe how easy that is. I was just about to start having to figure that out. Now my concern is how to pull data from between two sets of known characters instead of splitting based on a single delimeter. So if anyone has any ideas, I am all ears. Nothing jumps out at me in the API to do this easily. I can figure out the harder way, but didn't know if their was something as simple as splitting was for you.
Example:
hey
I would want to get the word 'hey'
|
|
|
04-12-2008, 08:59 PM
|
#4 (permalink)
|
|
Senior Member
Join Date: Apr 2008
Location: Colorado
Posts: 308
Rep Power: 0
|
Re: Pulling Data From A Web Page
Thanks for sharing. I was about to have to figure out how to do that.
|
|
|
04-15-2008, 03:01 PM
|
#5 (permalink)
|
|
Senior Member
Join Date: Apr 2008
Posts: 298
Rep Power: 0
|
Re: Pulling Data From A Web Page
Thought, would add more to it, here is method if you want to add escape characters to your string.
Code:
NSString *urlString = [myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString: urlString];
|
|
|
04-16-2008, 12:51 AM
|
#6 (permalink)
|
|
Junior Member
Join Date: Apr 2008
Posts: 1
Rep Power: 0
|
Re: Pulling Data From A Web Page
Quote:
|
Originally Posted by Cander
Thanks for that. I can't believe how easy that is. I was just about to start having to figure that out. Now my concern is how to pull data from between two sets of known characters instead of splitting based on a single delimeter. So if anyone has any ideas, I am all ears. Nothing jumps out at me in the API to do this easily. I can figure out the harder way, but didn't know if their was something as simple as splitting was for you.
Example:
hey
I would want to get the word 'hey'
|
You could use the method scanUpToCharactersFromSet:intoString: to find multiple characters, but that won't work for a lot of things, and probably won't work for you here. What you really want is to use regular expressions to do the parsing. AFAIK there is nothing in Coca to handle it, but you could use NSTask to call a command-line function like grep, or Python, Perl, or Ruby script (all of which have built-in support for REs), or use a third-party Cocoa framework.
Pretty odd that Cocoa has no regular expression support directly, a major oversight if you ask me (you'd think it would have been pretty obvious when they were coming up with NSScanner, even back in the NeXT days).
|
|
|
04-16-2008, 08:01 AM
|
#7 (permalink)
|
|
Junior Member
Join Date: Apr 2008
Posts: 7
Rep Power: 0
|
Re: Pulling Data From A Web Page
I got it working using NSScanner. Works perfectly.
Code:
- (void) getPilots:(NSString *)data {
NSString *foundData = @"";
NSScanner *scanner = [NSScanner scannerWithString:data];
// loop through the passed html data
while (![scanner isAtEnd]) {
NSInteger leftPos, rightPos;
[scanner scanUpToString: @"left text to find" intoString: nil]; leftPos = [scanner scanLocation];
if (![scanner scanUpToString: @"right text to fin" intoString: nil])
break;
rightPos = [scanner scanLocation] + 1;
leftPos += 19; // add the number of characters of the left text to find
// get data between the left and right text
foundData = [data substringWithRange: NSMakeRange(leftPos, (rightPos - leftPos) - 2)];
[playersArray addObject:foundData];
}
}
Yeah the lack of RegEx seems very odd. After years of using it in .NET I feel naked without it.
|
|
|
04-16-2008, 11:19 PM
|
#8 (permalink)
|
|
Senior Member
Join Date: Apr 2008
Location: Onomatopoeia, Lugubriousylvania
Posts: 225
Rep Power: 0
|
Re: Pulling Data From A Web Page
If your data is big, consider using NSURLConnection instead-- it allows you to load a URL asynchronously so that your UI doesn't completely lock up. Remember, your users might be stuck with the EDGE network for a while...
|
|
|
04-17-2008, 11:43 AM
|
#9 (permalink)
|
|
Junior Member
Join Date: Apr 2008
Posts: 7
Rep Power: 0
|
Re: Pulling Data From A Web Page
Quote:
|
Originally Posted by bonehead
If your data is big, consider using NSURLConnection instead-- it allows you to load a URL asynchronously so that your UI doesn't completely lock up. Remember, your users might be stuck with the EDGE network for a while...
|
Awesome. I was actually wondering about that last night on how not to lock up the view while loading.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
|
» Online Users: 184 |
| 11 members and 173 guests |
| Alexman, atsd, BostonMerlin, comtek, lepetitapps, Neverever, P-atr1k, Raphy, Slecorne, Stitch, _mubashir |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 8,172
Threads: 20,132
Posts: 89,979
Top Poster: RickMaddy (2,121)
|
| Welcome to our newest member, juicysen |
|