我正在尝试使用 AFNetworking 库在我的 iPhone 应用程序中加载一个简单的 JSON。我正在使用两种不同的方法:
NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/test.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Name: %@", [JSON valueForKeyPath:@"name"]);
} failure:^(NSURLRequest* req,NSHTTPURLResponse *req2, NSError *error,id mex) {
NSLog(@"%@", [error description]);
}];
[operation start];
并使用 AFHttpClient (这将是我最喜欢的,因为它具有处理我的 REST API 的所有功能:
NSString *baseurl = @"http://www.mysite.com";
NSString *path = @"/test.json";
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseurl]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
//[client setAuthorizationHeaderWithUsername:@"myusername" password:@"mypassword"];
[client getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
//NSLog(@"sjson: %@", [JSON valueForKeyPath:@"entries"]);
NSLog(@"sjson: %@", JSON);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error Code: %i - %@",[error code], [error localizedDescription]);
}];
现在,问题是:在这两种情况下,我都会收到一个空的 Json。我已经测试了服务器端,Web 服务给出了正确的响应和正确的 MIME 类型(应用程序/json)。
我已经检查了这个讨论https://github.com/AFNetworking/AFNetworking/issues/175 但我的目标是指向 5.0 并且没有为 AFNetworking 库启用 ARC。
也许我在这里遗漏了一些明显的东西,但我目前无法修复它。
非常感谢!
克劳斯