iOS:AFJSONRequestOperation NSInvalidArgumentException中的编码结果 - 'data parameter is nil'

时间:2013-04-23 10:27:47

标签: ios objective-c json encoding afjsonrequestoperation

我正在尝试获取一个JSon字符串,但有些单词有“á,é,ç”等。

JSon是:

{"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"}]}}

代码是:

-(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];
}

1 个答案:

答案 0 :(得分:0)

您的应用会抛出Exception,因为它无法从您请求的服务器获取数据。首先确保它接收一些数据,然后实现以下代码

尝试使用以下代码来解析您的json数据,我尝试使用我的应用程序,它也可以正常使用这些特殊符号。

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"]);
}