×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Objective-C
Posted by: Black Light
Added: Mar 3, 2013 12:06 PM
Views: 44
Tags: no tags
response in the form of String, JSON Array, JSON Object
  1. NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@webservice.php?param1=%@&param2=%@",kServerName, param1, param2]];
  2.             NSURLRequest *request=[NSURLRequest requestWithURL:url];
  3.             NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
  4.            
  5.             [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  6.                 if ([data length] >0 && error == nil)
  7.                 {
  8.                    
  9. // if response is a string
  10.                     NSString *responseString=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  11.  
  12. // if response is JSON Array
  13.  NSMutableArray *array=[NSMutableArray arrayWithArray:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]];
  14.                    
  15. // if response is JSON Object
  16.  NSMutableDictionary *dictionary=[NSMutableDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]];
  17.                    
  18.                  }
  19.                 else if ([data length] == 0 && error == nil)
  20.                 {
  21.                     NSLog(@"Nothing was downloaded.");
  22.                 }
  23.                 else if (error != nil)
  24.                 {
  25.                     NSLog(@"Error happened = %@", error);
  26.                 }
  27.                                    
  28.             }];