How to send data to web service using Afnetworking?

Asked

Viewed 119 times

1

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

How could I 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 the error message that appears:

Error Domain=Nscocoaerrordomain Code=3840 "The Operation couldn’t 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.}

2 answers

2


ok I was able to do this without using Parameters or having to convert content to JSON.

This is what I did, I created a URL complete, and passed to URLString: 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);
}];

1

The error indicates that the json sent by the server contains fragments, which are not supported by the parser by default. To solve the problem, include this code after initializing the Afhttprequestoperationmanager instance:

AFJSONResponseSerializer *serializer = [AFJSONResponseSerializer serializer];
[serializer setReadingOptions:NSJSONReadingAllowFragments];
[operationManager setResponseSerializer:serializer];
  • Good Rafael got the following error: Error Domain=NSCo coaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x7babefa0 {NSDebugDescription=Invalid value around character 0.}

  • I think it might be the server, because in the PHP file called, I’m using $var = $_GET("id")... to receive the data. Could that be it?

  • Funny, the app gives this error but registers the data correctly in the database! But I need to solve this problem because I need the SUCCESS or FAILURE feature to call the next screen or go back to the previous one :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.