我正在使用通过 Cocoapods 添加到我的项目中的 AFNetworking 2.0.0-RC2。尝试使用我的第一种方法来调用 Web 服务,查看 AFNetworking 中的示例项目,它看起来不错。但由于我的项目的某种原因,我无法让它工作。
DNAAppDelegate.h:
#import <UIKit/UIKit.h>
@interface DNAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
+ (void)globalTimelinePostsWithBlock:(void (^)(NSArray *posts, NSError *error))block;
@end
DNAAppDelegate.m:
#import "DNAppDelegate.h"
#import "AFNetworking.h"
#import "AFNetworkActivityIndicatorManager.h"
#import "DNHackerNewsAPIClient.h"
@class DNHackerNewsAPIClient;
@implementation DNAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//AFNetworking setup
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
//Call API
//Window work
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
+ (void)globalTimelinePostsWithBlock:(void (^)(NSArray *posts, NSError *error))block {
// ^ ERROR HAPPENS ON THIS METHOD
[[DNHackerNewsAPIClient sharedClient] getPath:@"page" parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
NSArray *postsFromResponse = [JSON valueForKeyPath:@"data"];
// if (block) {
// block([NSArray arrayWithArray:mutablePosts], nil);
// }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (block) {
block([NSArray array], error);
}
}];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
编辑:
DNHackerNewsAPIClient.h:
#import <Foundation/Foundation.h>
#import "AFHTTPClient.h"
@interface DNHackerNewsAPIClient : AFHTTPClient
+ (DNHackerNewsAPIClient *)sharedClient;
@end
DNHackerNewsAPIClient.m:
#import "DNHackerNewsAPIClient.h"
#import "AFJSONRequestOperation.h"
static NSString * const kDNHackerNewsAPIBaseURLString = @"http://api.ihackernews.com/";
@implementation DNHackerNewsAPIClient
//Setup the singleton to use throught the life of the application
+ (DNHackerNewsAPIClient *)sharedClient {
static DNHackerNewsAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[DNHackerNewsAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kDNHackerNewsAPIBaseURLString]];
});
return _sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
// [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
//
// // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
// [self setDefaultHeader:@"Accept" value:@"application/json"];
return self;
}
@end
我错过了什么?
编辑2:
为什么这似乎永远不会触发?
NSURL *url = [NSURL URLWithString:@"http://api.ihackernews.com/"];
DNHackerNewsAPIClient *HNClient = [[DNHackerNewsAPIClient alloc] initWithBaseURL:url];
[HNClient
GET:@"page"
parameters:nil
success:^(NSHTTPURLResponse *response, id responseObject)
{
NSLog(@"It Worked: %@", response);
NSLog(@"Y U NO WORK.");
}
failure:^(NSError *error) {
NSLog(@"It Failed: %@", error);
}];