Home News Forum Social Networking Support Us Advertise

Spanish Lesson 1 ($1.99)

aWake!Gently ($1.99)

The Bird & The Snail - Knock Knock - Deluxe ($4.99)

Match-It Trains ($0.99)

Tangled ($0.99)

iFlatter ($0.99)

The 15 puzzle ($0.99)

Tap Forms Database ($8.99)

Higher or Lower Card Game (Hi Lo) ($0.99)

Red Pixel ($0.99)

Time-Shift Radio ($0.99)

Want your application advertised here? Only $10/week!

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-11-2008, 07:40 PM   #1 (permalink)
CoffeeKid
Guest
 
Posts: n/a
Default 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
  Reply With Quote
Old 04-11-2008, 10:02 PM   #2 (permalink)
CoffeeKid
Guest
 
Posts: n/a
Default 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
  Reply With Quote
Old 04-12-2008, 02:13 AM   #3 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0
Cander
Default 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'
Cander is offline   Reply With Quote
Old 04-12-2008, 08:59 PM   #4 (permalink)
Senior Member
 
Join Date: Apr 2008
Location: Colorado
Posts: 308
Rep Power: 0
jonc
Send a message via AIM to jonc
Default Re: Pulling Data From A Web Page

Thanks for sharing. I was about to have to figure out how to do that.
__________________
xCodeForum
jonc is offline   Reply With Quote
Old 04-15-2008, 03:01 PM   #5 (permalink)
Senior Member
 
Join Date: Apr 2008
Posts: 298
Rep Power: 0
javid.alimohideen is an unknown quantity at this point
Default 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];
javid.alimohideen is offline   Reply With Quote
Old 04-16-2008, 12:51 AM   #6 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 1
Rep Power: 0
HiRez
Default 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).
HiRez is offline   Reply With Quote
Old 04-16-2008, 08:01 AM   #7 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0
Cander
Default 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.
Cander is offline   Reply With Quote
Old 04-16-2008, 11:19 PM   #8 (permalink)
Senior Member
 
Join Date: Apr 2008
Location: Onomatopoeia, Lugubriousylvania
Posts: 225
Rep Power: 0
bonehead
Default 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...
bonehead is offline   Reply With Quote
Old 04-17-2008, 11:43 AM   #9 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0
Cander
Default 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.
Cander is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting data from an UIImage realberen iPhone SDK Development 11 03-16-2009 01:43 AM
Need Help Posting an image from iPhone to php page via POST ddavtian iPhone SDK Development 4 09-07-2008 08:08 PM
pixel data iamdave iPhone SDK Development 2 08-12-2008 08:38 PM
UIWebView how to reload page every time Marco iPhone SDK Development 1 07-06-2008 12:38 AM
web page search nownot iPhone SDK Development 0 07-04-2008 12:30 PM

» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:17 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0