Posted By

saturngod on 02/21/11


Tagged


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

concertman
Tintenklecks
cnathan


asynchronous connection


 / Published in: Objective C
 

  1. //
  2. // RequestDataViewController.m
  3. // RequestData
  4. //
  5. // Created by EDENCR8 on 2/17/11.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "RequestDataViewController.h"
  10.  
  11. @implementation RequestDataViewController
  12. @synthesize receiveData;
  13. @synthesize resultView;
  14.  
  15.  
  16. /*
  17. // The designated initializer. Override to perform setup that is required before the view is loaded.
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  19.   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  20.   if (self) {
  21.   // Custom initialization
  22.   }
  23.   return self;
  24. }
  25. */
  26.  
  27. /*
  28. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  29. - (void)loadView {
  30. }
  31. */
  32.  
  33.  
  34. /*
  35. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  36. - (void)viewDidLoad {
  37.   [super viewDidLoad];
  38. }
  39. */
  40.  
  41.  
  42. /*
  43. // Override to allow orientations other than the default portrait orientation.
  44. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  45.   // Return YES for supported orientations
  46.   return (interfaceOrientation == UIInterfaceOrientationPortrait);
  47. }
  48. */
  49.  
  50. - (void)didReceiveMemoryWarning {
  51. // Releases the view if it doesn't have a superview.
  52. [super didReceiveMemoryWarning];
  53.  
  54. // Release any cached data, images, etc that aren't in use.
  55. }
  56.  
  57. - (void)viewDidUnload {
  58. // Release any retained subviews of the main view.
  59. // e.g. self.myOutlet = nil;
  60. }
  61.  
  62.  
  63. -(void)viewDidLoad
  64. {
  65. NSLog(@"started");
  66. CGRect resultFrame=CGRectMake(10, 10, 300, 100);
  67. resultView=[[UITextView alloc] initWithFrame:resultFrame];
  68. resultView.font=[UIFont systemFontOfSize:14.0];
  69. [self.view addSubview:resultView];
  70.  
  71. NSString *urlString=@"http://192.168.1.67/dbs/location.xml";
  72. NSLog(@"%@",urlString);
  73.  
  74. NSURL *url=[NSURL URLWithString:urlString];
  75.  
  76. receiveData=[[NSMutableData alloc] initWithData:nil];
  77.  
  78.  
  79. NSURLRequest *req=[[NSURLRequest alloc] initWithURL:url];
  80. NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:req delegate:self];
  81. [req release];
  82. [conn release];
  83.  
  84.  
  85. }
  86.  
  87. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  88. NSLog(@"receive data");
  89. [receiveData appendData:data];
  90. }
  91.  
  92. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  93. UIAlertView *myAlert=[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  94. [myAlert show];
  95. [myAlert release];
  96. }
  97.  
  98. -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
  99. NSString* quotes=[[NSString alloc] initWithBytes:[receiveData bytes] length:[receiveData length] encoding:NSASCIIStringEncoding];
  100. resultView.text=quotes;
  101. [quotes release];
  102. }
  103.  
  104. - (void)dealloc {
  105. [super dealloc];
  106. [receiveData release];
  107. }
  108.  
  109. @end

Report this snippet  

You need to login to post a comment.