0

我想用最新的 AFNetworking 版本 2 更新我的应用程序。从现在起他们改变了一些东西,我想知道如何下载 plist 文件。

我在文档中找到了这个例子:

NSURL *URL = [NSURL URLWithString:@"http://example.com/foo.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                     initWithRequest:request];
operation.responseSerializer = [AFJSONSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", responseObject);
} failure:nil];
[operation start];

但我需要下载一个 plist 我对 AFNetworking 1 所做的事情,如下所示:

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/test.plist"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
    AFPropertyListRequestOperation *operation = [AFPropertyListRequestOperation propertyListRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id propertyList) {

        NSDictionary *myTempDic = (NSDictionary *)propertyList;
        myArray = [myTempDic objectForKey:@"Whatever"];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id propertyList) {
//do something with the error
}];
[operation start];

我在哪里可以找到有关使用 AFNetworking 2.0 处理 Plist 的任何示例?

我找到了这种方法。这个对吗?

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFPropertyListResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id propertyList) {

    NSDictionary *myTempDic = (NSDictionary *)propertyList;
    myArray = [myTempDic objectForKey:@"Whatever"];

    }failure:nil];

    [operation start];
4

1 回答 1

2

我只是在http://www.raywenderlich.com/30445/上做 AFnetworking 教程,从 1.0 到 2.0 时遇到了同样的问题

我还很新,但这是我发现可行的解决方案:

 NSString *url = @"http://example.com/foo.plist";

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

 manager.responseSerializer = [AFPropertyListResponseSerializer serializer];

 [manager GET:url 
   parameters:nil 
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          myArray = [responseObject objectForKey:@"Whatever"]; //responseObject is a dictionary
          NSLog(@"PLIST: %@", responseObject);
      } 
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      }];
于 2013-10-28T15:26:22.650 回答