我有一个UITableViewController,我正在尝试使用此代码从url获取JSON数据但我收到错误。我该怎么办?
AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
[securityPolicy setAllowInvalidCertificates:YES];
NSString *urlPath = [NSString stringWithFormat:@"http://testurl.com/api/index.php"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"process":@"search_customer",@"page": @""};
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager setSecurityPolicy:securityPolicy];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:urlPath parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSData *jsonData = [NSData dataWithContentsOfFile:string];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.customerCards = [NSMutableArray array];
NSArray *customersArray = [dataDictionary objectForKey:@"musteri_list"];
for (NSDictionary *customersDictionary in customersArray) {
ApiClass *customer = [ApiClass customersWithTitle:[customersDictionary objectForKey:@"adi"]];
customer.tel = [customersDictionary objectForKey:@"tel"];
[self.customerCards addObject:customer];
}
NSLog(@"GET: %@", string);
NSLog(@"POST: %@", parameters);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
这是错误集团
2015-03-26 14:27:37.656 SaphiraCrm[13770:3881729] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
答案 0 :(得分:0)
我认为问题可能在于您将responseObject
视为NSData
,但根据AFNetworking
examples,默认情况下{{} 1}} AFHTTPRequestOperationManager
是responseSerializer
,因此AFJSONResponseSerializer
已经是一个解析的JSON,表示为responseObject
(取决于序列化程序)。
因此,行
NSDictionary
返回NSString *string = [[NSString alloc] initWithData:responseObject
encoding:NSUTF8StringEncoding];
,这会导致下一行
nil
提出异常。
我建议您检查NSData *jsonData = [NSData dataWithContentsOfFile:string];
是否符合预期,并相应地重构您的代码。
responseObject
注意:强>
上述情况很可能只是您设置seriliazer配置的前一个错误的结果,因为您首先先设置...
[manager POST:urlPath
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject) {
NSLog(@"Response is a %@ and contains: %@", [responseObject class], responseObject);
// Process responseObject according to its type
...
,然后使用默认值覆盖它。
acceptableContentType
答案 1 :(得分:0)
Okey我想出了我该怎么做。
有我的代码
AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
[securityPolicy setAllowInvalidCertificates:YES];
NSString *urlPath = [NSString stringWithFormat:@"http://test.com/api/index.php"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"process":@"search_customer",@"page": @""};
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager setSecurityPolicy:securityPolicy];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:urlPath parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSError *error = nil;
NSData *jsonData = [string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.customerCards = [NSMutableArray array];
NSArray *customersArray = [dataDictionary objectForKey:@"musteri_list"];
NSLog(@"%@",customersArray);
for (NSDictionary *customersDictionary in customersArray) {
ApiClass *customer = [ApiClass customersWithTitle:[customersDictionary objectForKey:@"adi"]];
customer.tel = [customersDictionary objectForKey:@"tel"];
[self.customerCards addObject:customer];
}
[self.tableView reloadData];
NSLog(@"GET: %@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];