使用AFJSONRequestOperation返回JSON

jcr*_*son 2 iphone objective-c ios afnetworking

我已经实现了AFNetworking框架,但是我试图想出一种方法将AFJSONRequestOperation函数放在一个单独的类中并从我的各个视图控制器中调用它.

我所做的是创建一个类方法,它接受字典中的参数和webservice url.我希望这能够从成功块返回JSON数组和Http代码,但这不起作用.

如果我将公共函数更改为具有返回类型并在方法结束时返回值,则它将在成功块完成之前返回.

有什么建议?

+ (void)RequestJSON:(NSDictionary *)params:(NSString *)webServicePath {

    NSURL *url = [NSURL URLWithString:@"http://api.website.com/"];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:webServicePath parameters:params];
    NSLog(@"%@", request);

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request

    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
                                         {

                                           NSString *httpCode = [[JSON valueForKey:@"meta"]valueForKey:@"code"];
                                             NSLog(@"code=%@", httpCode);

                                         }

    failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON)
                                         {
                                             NSLog(@"Failed: %@",[error localizedDescription]);

                                         }];
    [operation start];

}
Run Code Online (Sandbox Code Playgroud)

jcr*_*son 5

根据其他用户的推荐,我最终创建了自己的Delegate,它在成功和失败方法中执行两个选择器,didReceiveJSON和didNotReceiveJSON.

编辑:这是我的代码:)

JSONRequest.h

#import <Foundation/Foundation.h>

@class JSONRequest;

@protocol JSONRequest <NSObject>

- (void)didReceiveJSONResponse:(NSDictionary*)JSONResponse;
- (void)didNotReceiveJSONResponse;

@end

@interface JSONRequest : NSObject {

    id <JSONRequest> delegate;
}

@property (strong, nonatomic) id <JSONRequest> delegate;

- (void)RequestJSON:(NSDictionary* )params:(NSString*)webServicePath;

@end
Run Code Online (Sandbox Code Playgroud)

JSONRequest.m

#import "JSONRequest.h"
#import "AFHTTPClient.h"
#import "AFJSONRequestOperation.h"

@implementation JSONRequest
@synthesize delegate;

- (void)RequestJSON:(NSDictionary *)params:(NSString *)webServicePath {

    NSURL *url = [NSURL URLWithString:@"http://api.mysite.com"];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:webServicePath parameters:params];
    NSLog(@"%@", request);

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request

    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
                                         {
                                             NSDictionary *jsonDictionary = JSON;
                                             [delegate performSelector:@selector(didReceiveJSONResponse:) withObject:jsonDictionary];
                                         }

    failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON)
                                         {
                                             NSLog(@"Failed: %@",[error localizedDescription]);
                                             [delegate performSelector:@selector(didNotReceiveJSONResponse)];

                                         }];
    [operation start];

}
Run Code Online (Sandbox Code Playgroud)

@结束