3

Actually I am using now JSON classes for calling web-services but now i want to call that webservice using NSURLConnection any one provide me code for that.

Please provide me details of frameworks what i have to import.

Thank you in advance.

Michaël Azevedo
  • 3,874
  • 7
  • 31
  • 45
Hardik Vyas
  • 1,973
  • 1
  • 20
  • 43
  • "NSURLConnection + JSON" didn't give you responses? One of the first link on Google: http://stackoverflow.com/questions/20995815/nsurlconnection-getting-json-file – Larme Jun 17 '16 at 12:09
  • You can use AFNetworking library for web service call. – Jayesh Thanki Jun 17 '16 at 12:09
  • 1
    Why are you using `NSURLConnection`? its Deprecated in iOS 9.0. Instead use `NSURLSession` for the same. You can refer this nice [tutorial on NSURLSession](https://www.objc.io/issues/5-ios7/from-nsurlconnection-to-nsurlsession/) by Mattt Thompson. – Dipen Panchasara Jun 17 '16 at 12:41

3 Answers3

6
NSURL *url = [NSURL URLWithString:stringurl];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
     NSLog(@"%@",dictionary);
 }];

You Can use this.

abhishekkharwar
  • 3,529
  • 2
  • 18
  • 31
Hardik Chapla
  • 435
  • 6
  • 26
  • But strongly discouraged for new code. ``[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {...}];`` is preferred. – dgatwood Jun 29 '16 at 02:16
2

You can Do like this using Synchronous :

NSURL *url=[NSURL URLWithString:urlString];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
NSData *data=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSString *response=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dd=[response JSONValue];

OR Using Delegate Method

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSURLResponse *response = nil;


 // Create url connection and fire request
       NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
      [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];


#pragma mark NSURLConnection Delegate Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    // A response has been received, this is where we initialize the instance var you created
    // so that we can append data to it in the didReceiveData method
    // Furthermore, this method is called each time there is a redirect so reinitializing it
    // also serves to clear it

    _responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // Append the new data to the instance variable you declared
    NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
                                                         options:kNilOptions
                                                           error:&error];

   // NSArray* latestLoans = [json objectForKey:@"loans"];

    NSLog(@"json: %@", json);


    [_responseData appendData:data];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse*)cachedResponse {
    // Return nil to indicate not necessary to store a cached response for this connection
    return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // The request is complete and data has been received
    // You can parse the stuff in your instance variable now

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // The request has failed for some reason!
    // Check the error var
}
sohil
  • 818
  • 2
  • 15
  • 38
0

Use below code forrcalling SOAP web service (POST) :

-(NSString *)posturl:(NSString *)url withpoststring:(NSString *)postString {

   NSString *post = postString;
   NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
   NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
   NSString *URL = url;
   NSLog(@"%@", URL);
   NSLog(@"%@",post);

   [request  setURL:[NSURL URLWithString:URL]];
   [request setHTTPMethod:@"POST"];
   [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
   [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
   [request setHTTPBody:postData];

   NSError *error;
   NSURLResponse *response;
   NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
   NSString *data = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

   if ([data isEqualToString:@""]) {

   } else {
      data = stringByStrippingHTML(data);
   }

   return data;
}