Search code examples
iosobjective-cafnetworking-3

AFNetworking 3.0 not fetching the results


Here is how I'm sending request to the server:

- (void)searchForText:(NSString *)searchText scope:(NSInteger)scopeOption
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showWithStatus:@"Getting Data"];

    NSString *string = [NSString stringWithFormat:@"%@/API/ICD10Code/1",BaseURLString];
    NSDictionary *para = @{@"Code":searchText};

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"PUT" URLString:string parameters:para error:nil];

    NSString *token = [NSString stringWithFormat:@"Bearer %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userToken"]];
    [req setValue:token forHTTPHeaderField:@"Authorization"];

    NSLog(@"URL is: %@ Parameters: %@ Token: %@",string, para, token);

    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error){
        if (!error) {
            if (response) {

                NSLog(@"Response is: %@",response);
                [SVProgressHUD dismiss];
            }
        } else {
            // NSLog(@"Error: %@, %@, %@", error, response, responseObject);
            NSLog(@"Error: %@", error.localizedDescription);
            [SVProgressHUD dismiss];
            SCLAlertView *alert = [[SCLAlertView alloc] init];

            [alert showError:self title:@"Error"
                    subTitle:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"Message"]]
            closeButtonTitle:@"OK" duration:0.0f];
        }
    }] resume];
}

When I run the code the NSLog of response is giving me:

Response is: { URL: http://ts-test01/app/API/ICD10Code/1 } { status code: 200, headers { "Cache-Control" = "no-cache"; "Content-Length" = 1525; "Content-Type" = "application/json; charset=utf-8"; Date = "Wed, 05 Oct 2016 09:50:15 GMT"; Expires = "-1"; Pragma = "no-cache"; Server = "Microsoft-IIS/8.0"; "X-AspNet-Version" = "4.0.30319"; "X-Powered-By" = "ASP.NET"; } }

But the backend guy is getting data. What am I doing wrong in sending the request.


Solution

  • you wrongly print the response ,use responseObject for fetch the output

    in this place

      NSLog(@"Response is: %@",response);
    

    use

     NSLog(@"output== %@ ", responseObject);
    

    for sample

    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error){
               [SVProgressHUD dismiss];
    
            if (!error) {
                if (response) {
    
    
                     NSLog(@"output== %@", responseObject);
    
             if ([responseObject isKindOfClass:[NSDictionary class]]) {
              //blah blah
               }
                }
            } else {
                // NSLog(@"Error: %@, %@, %@", error, response, responseObject);
                NSLog(@"Error: %@", error.localizedDescription);
                SCLAlertView *alert = [[SCLAlertView alloc] init];
    
                [alert showError:self title:@"Error"
                        subTitle:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"Message"]]
                closeButtonTitle:@"OK" duration:0.0f];
            }
        }] resume];