Sergio Costa Faria
Sergio Costa Faria

Reputation: 57

iOS: AFJSONRequestOperation Encoding result in NSInvalidArgumentException - 'data parameter is nil'

I'm trying to get a JSon string but some words has "á, é, ç", etc.

The JSon is:

{"Error":"", "Result":{"Transacoes": [{"ClienteID":"1","ID":"915","Banco":"Bradesco","Fornecedor":"","Tipo":"Cartão de crédito - Bradesco Visa","ValorCredito":"0,0000","ValorDebito":"4000,0000","Vencimento":"","Pagamento":"03/08/2012 00:00:00","Descricao":"Cartão Bradesco Visa","Lançamento":"03/08/2012 15:18:12"},{"ClienteID":"1","ID":"916","Banco":"Bradesco","Fornecedor":"","Tipo":"Alinhamento da Conta Bancária","ValorCredito":"22398,9200","ValorDebito":"0,0000","Vencimento":"","Pagamento":"02/08/2012 00:00:00","Descricao":"FGTS","Lançamento":"07/08/2012 11:12:16"}]}}

And the code is:

-(void)viewWillAppear:(BOOL)animated {
NSString *urlAddress = [NSString stringWithFormat:@"http://SITE?action=transacoes&AuthToken=%@&DataDe=02/08/2012&DataAte=03/08/2012", self.usuario.authToken];

NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest: request
                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"%@", JSON);
                                        }
                                        failure: ^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON){
                                            NSLog(@"ERROR: %@", error);
                                        }];

[operation start];
}

Upvotes: 0

Views: 253

Answers (1)

Dipen Panchasara
Dipen Panchasara

Reputation: 13600

Your app throws Exception because it can't get data from server ,which you are requesting. SO first make sure it receives some data and then implement following code

Try Following code to parse your json data, i tried with my app, its working fine with that special symbols as well.

NSString *urlAddress = [NSString stringWithFormat:@"http://SITE?action=transacoes&AuthToken=%@&DataDe=02/08/2012&DataAte=03/08/2012", self.usuario.authToken];

NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSHTTPURLResponse* urlResponse = nil;

NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

if([responseData bytes] > 0)
{
    // It will work only IOS 5.0 or later version otherwise you can use any third party JSON parsers (eg. SBJSON)
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData
                          options:NSJSONReadingMutableLeaves
                          error:&error];
    NSLog(@"%@",[[[[json objectForKey:@"Result"] objectForKey:@"Transacoes"] objectAtIndex:1] valueForKey:@"Descricao"]);
    NSLog(@"%@",[[[[json objectForKey:@"Result"] objectForKey:@"Transacoes"] objectAtIndex:1] valueForKey:@"Tipo"]);
}

Upvotes: 0

Related Questions