Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created October 19, 2012 00:11
Show Gist options
  • Save ChrisRisner/3915526 to your computer and use it in GitHub Desktop.
Save ChrisRisner/3915526 to your computer and use it in GitHub Desktop.
ios day 7
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <NSURLConnectionDelegate> {
NSMutableData* _receivedData;
}
@property (weak, nonatomic) IBOutlet UITextField *txtName;
@property (weak, nonatomic) IBOutlet UILabel *lblData;
- (IBAction)btnFetchData1:(id)sender;
- (IBAction)txtFetchData2:(id)sender;
@end
- (IBAction)txtFetchData2:(id)sender {
NSString *queryString = [NSString stringWithFormat:@"http://chrisrisner.com/Labs/day7test.php?name=%@", [self.txtName text]];
NSURLRequest *theRequest=[NSURLRequest
requestWithURL:[NSURL URLWithString:
queryString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
//do something with error
} else {
NSString *responseText = [[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding];
NSLog(@"Response: %@", responseText);
NSString *newLineStr = @"\n";
responseText = [responseText stringByReplacingOccurrencesOfString:@"<br />" withString:newLineStr];
[self.lblData setText:responseText];
}
}];
}
-(void)connection:(NSConnection*)conn didReceiveResponse:(NSURLResponse *)response
{
if (_receivedData == NULL) {
_receivedData = [[NSMutableData alloc] init];
}
[_receivedData setLength:0];
NSLog(@"didReceiveResponse: responseData length:(%d)", _receivedData.length);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error {
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Succeeded! Received %d bytes of data",[_receivedData length]);
NSString *responseText = [[NSString alloc] initWithData:_receivedData encoding: NSASCIIStringEncoding];
NSLog(@"Response: %@", responseText);
NSString *newLineStr = @"\n";
responseText = [responseText stringByReplacingOccurrencesOfString:@"<br />" withString:newLineStr];
[self.lblData setText:responseText];
}
- (IBAction)btnFetchData1:(id)sender {
NSString *queryString = [NSString stringWithFormat:@"http://chrisrisner.com/Labs/day7test.php?name=%@",
[self.txtName text]];
NSURLRequest *theRequest=[NSURLRequest
requestWithURL:[NSURL URLWithString:
queryString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (con) {
_receivedData=[NSMutableData data];
} else {
//something bad happened
}
}
- (IBAction)btnFetchData1:(id)sender {
NSURLRequest *theRequest=[NSURLRequest
requestWithURL:[NSURL URLWithString:
@"http://chrisrisner.com/Labs/day7test.php"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (con) {
_receivedData=[NSMutableData data];
} else {
//something bad happened
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment