AFNetworking返回(null)

时间:2014-08-22 20:22:35

标签: ios objective-c json

我已经构建了一个代码,用于从Web上的JSON文件中获取数据。 我想获取JSON文件的内容并打印它(通过NSLog)。

但是,有一个问题。程序返回null .. 这就是所说的:DataString: (null)。 为什么会这样?我想要JSON文件的内容..

以下是代码:

- (void)viewDidLoad
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString * requestURL = [NSString stringWithFormat:@"http://www.oref.org.il/WarningMessages/alerts.json"];

    AFHTTPRequestOperation *op = [manager GET:requestURL parameters:nil

      success:^(AFHTTPRequestOperation *operation, id responseObject) { //if success
          NSData * getData = operation.responseData;
          NSDictionary * jsonResponse = [NSJSONSerialization JSONObjectWithData:(getData) options:kNilOptions error:nil];
          NSString * idFromJson = jsonResponse[@"id"];
          NSLog(@"DataString: %@", operation.responseString);
          //NSLog(@"Data: %@", getData);

          UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Loaded" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil , nil];
          [alert show];
      }

      failure:^(AFHTTPRequestOperation *operation, NSError *error) { // if failed
          NSLog(@"Error: %@", error);

          UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Error loading data. Please check your internet connection or try again later." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil , nil];
          [alert show];

      }];

    [op start];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

OP的评论中添加:
现在,我明白了:DataString:

<fffe7b00 20000d00 0a002200 69006400 22002000 3a002000 22003100 34003000 38003700 34003900 37003300 39003300 33003400 22002c00 0d000a00 22007400 69007400 6c006500 22002000 3a002000 2200e405 d905e705 d505d305 2000d405 e205d505 e805e305 2000d405 ea05e805 e205d405 2000d105 de05e805 d705d105 20002200 2c000d00 0a002200 64006100 74006100 22002000 3a002000 5b005d00 0d000a00 7d000d00 0a000d00 0a00>

我的回复代码是200,因为我在以色列。 问题是我无法获取.json文件内容。

3 个答案:

答案 0 :(得分:3)

鉴于@Itay Pincas的数据是对@Dima Goltsman的评论

数据采用带有BOM格式的UTF-16,这很不寻常,因为UTF-8是一般的响应格式。关键是前两个字节:fffe

// Create the data from the question:  
uint16_t utf16Bytes[] = {0xfffe, 0x7b00, 0x2000, 0x0d00, 0x0a00, 0x2200, 0x6900, 0x6400, 0x2200, 0x2000, 0x3a00, 0x2000, 0x2200, 0x3100, 0x3400, 0x3000, 0x3800, 0x3700, 0x3400, 0x3900, 0x3700, 0x3300, 0x3900, 0x3300, 0x3300, 0x3400, 0x2200, 0x2c00, 0x0d00, 0x0a00, 0x2200, 0x7400, 0x6900, 0x7400, 0x6c00, 0x6500, 0x2200, 0x2000, 0x3a00, 0x2000, 0x2200, 0xe405, 0xd905, 0xe705, 0xd505, 0xd305, 0x2000, 0xd405, 0xe205, 0xd505, 0xe805, 0xe305, 0x2000, 0xd405, 0xea05, 0xe805, 0xe205, 0xd405, 0x2000, 0xd105, 0xde05, 0xe805, 0xd705, 0xd105, 0x2000, 0x2200, 0x2c00, 0x0d00, 0x0a00, 0x2200, 0x6400, 0x6100, 0x7400, 0x6100, 0x2200, 0x2000, 0x3a00, 0x2000, 0x5b00, 0x5d00, 0x0d00, 0x0a00, 0x7d00, 0x0d00, 0x0a00, 0x0d00,  0x0a00};

使用encoding:NSUTF16StringEncoding解码:

NSData *data = [NSData dataWithBytes:utf16Bytes length:sizeof(utf16Bytes)];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding];
NSLog(@"str: '%@'", str);

输出:

str: '{
    "id" : "1408749739334",
    "title" : "פיקוד העורף התרעה במרחב ",
    "data" : []
}

对于字典对象:

NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

NSLog(@"id:    %@", dict[@"id"]);
NSLog(@"title: %@", dict[@"title"]);
NSLog(@"data:  %@", dict[@"data"]);

输出:

title: פיקוד העורף התרעה במרחב
id:    1408749739334
data:  ()

来自JSONObjectWithData文档:

  

流中的数据必须是5种支持的编码之一   在JSON规范中列出:UTF-8,UTF-16LE,UTF-16BE,UTF-32LE,   UTF-32BE。数据可能有也可能没有BOM。效率最高   用于解析的编码是UTF-8,所以如果你有选择   编码传递给此方法的数据,使用UTF-8。

答案 1 :(得分:1)

您的网址仅在以色列拥有访问权限。如果您的服务器位于其他位置,或者您向此网址发送了太多请求,则会收到Access Denied响应。

您的回复代码可能是403,您可以使用

获取

NSInteger statusCode = operation.response.statusCode;

答案 2 :(得分:0)

网址要求访问权限。您没有在请求中包含这些内容。