How to send data to web service using AFNetworking?

1

I have a form that must be completed in the application and sent to the web service to register a new user.

How could you send this data to the web service using AFNetworking 2.0?

I tried to use the code below, but returned an error:

NSDictionary *params =      @{@"id" : idCadastro.text,
                             @"nome" : nomeCadastro.text,
                             @"email" : emailCadastro.text,
                             @"cidade" : localidadeCadastro.text,
                             @"passe" : senhaCadastro.text
                                                         };
    NSString* HOST_URL = @"http://10.1.1.6/advphp/cad_adv_0.php?";


    AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
    [operationManager POST: HOST_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){

        // Enter what happens here if successsful.
        NSLog(@"Cadastrado");

    }failure:^(AFHTTPRequestOperation *operation, NSError *error){

        // Enter what happens here if failure happens

        NSLog(@"Não Cadastrado. Erro:\n%@",error);
    }
     ];

And below is the error message that appears:

  

Error Domain = NSCocoaErrorDomain Code = 3840 "The operation could not be   completed. (Cocoa error 3840.) "(JSON text did not start with array or   object and option to allow fragments not set.) UserInfo = 0x7969de10   {NSDebugDescription = JSON text did not start with array or object and   option to allow fragments not set.}

    
asked by anonymous 05.11.2014 / 19:07

2 answers

2

Ok I managed to do this without using the parameters or having to convert the contents to JSON.

I did the following, I created a% complete%, and I went to URL : directly!

NSMutableString* urlStrin = [[NSMutableString alloc]initWithFormat:@"http://10.1.1.6/advphp/cadastrar.php?id=%@&nome=%@&email=...%@",idCadastro.text, nomeCadastro.text, emailCadastro.text, localidadeCadastro.text, senhaCadastro.text];


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

[manager GET:urlStrin parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
// do whatever you'd like here; for example, if you want to convert
// it to a string and log it, you might do something like:

NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
    
05.11.2014 / 22:13
1

The error indicates that the json sent by the server contains fragments, which are not supported by the parser by default. To resolve the problem, include this code after the AFHTTPRequestOperationManager instance is initialized:

AFJSONResponseSerializer *serializer = [AFJSONResponseSerializer serializer];
[serializer setReadingOptions:NSJSONReadingAllowFragments];
[operationManager setResponseSerializer:serializer];
    
05.11.2014 / 19:37