使用AFNetworking

时间:2015-07-27 09:06:14

标签: ios iphone afnetworking-2

我有一个包含json文件的链接。我的意思是,如果我在chrome中启动该链接,则会在我的计算机上下载.json扩展名。 说链接是www.foffa.com/sampleList.json 我是AFNetworking的新手,不知道如何解析这个json,无论是否必须将此文件写入磁盘。

我的代码如下所示,我很确定我必须使用流和所有这些,但我不知道如何。截至目前,我收到错误"Request failed: unacceptable content-type: text/plain" 我想它希望内容类型为"Content-Type" = "application/octet-stream";

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFHTTPResponseSerializer serializer];
    operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/octet-stream"];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"Data retrived");       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];

2 个答案:

答案 0 :(得分:2)

更改第二行:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFHTTPResponseSerializer serializer];

到此:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];

请注意,您使用序列化的AFJSONResponseSerializer响应。

<强>更新

在下面的注释中从info更新我的答案,看来你真的想从服务器下载一个JSON文件然后解析它,而不是直接从HTTP响应中解析JSON:

为了做到这一点,请将内容类型更改为text/plain,并将已下载的file/data解析为StringJSON对象,如下所示:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:nil];
operation.responseSerializer = [AFHTTPResponseSerializer serializer];
operation.responseSerializer.acceptableContentTypes =
    [NSSet setWithObjects:@"application/octet-stream", @"text/plain", nil];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  NSError *error;
  id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error];
  if (error) {
      NSLog(@"Error: %@", error);
  } else {
      NSLog(@"JSON: %@", json);
  }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                      message:[error localizedDescription]
                                                     delegate:nil
                                            cancelButtonTitle:@"Ok"
                                            otherButtonTitles:nil];
  [alertView show];
}];

答案 1 :(得分:0)

AFNetwroking不会为您解析收入数据。

使用NSJSONSerialization的类方法+JSONObjectWithData:options:error:。如果您的数据有效,则结果将是NSDictionary,其结构与原始JSON文件相同

你应该把它放在成功块