首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用AFJSONRequestOperation返回JSON

使用AFJSONRequestOperation返回JSON
EN

Stack Overflow用户
提问于 2012-12-07 21:20:37
回答 4查看 4.7K关注 0票数 2

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

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

如果我将公共函数更改为具有返回类型,并在方法的末尾返回值,则它会在成功块完成之前返回。

有什么建议吗?

代码语言:javascript
复制
+ (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];

}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-12-07 22:56:58

根据其他用户的建议,我最终创建了自己的委托,它在success和failure方法中执行两个选择器didReceiveJSON和didNotReceiveJSON。

编辑:以下是我的代码:)

代码语言:javascript
复制
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

JSONRequest.m

代码语言:javascript
复制
#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];

}

@end

票数 5
EN

Stack Overflow用户

发布于 2012-12-07 21:37:33

您的AFJSONRequestOperation异步运行!所以它不是在主线程上运行(我认为这就是AFJSONRequestOperation的目的),但是您的+ (void)RequestJSON:(NSDictionary *)params:(NSString *)webServicePath方法是在主线程上执行的,因此会立即完成。例如,here's某人问同样的问题。

因为AFJSONRequestOperation是基于NSOperation的,所以可以将其添加到mainQueu中。但是,从主线程发送同步请求并不是一个好主意。但也许你可以看看this workaround

票数 0
EN

Stack Overflow用户

发布于 2012-12-07 21:37:49

该函数返回是因为它的任务只是设置OperationQueue。一旦它这样做了,它就会像往常一样继续它的流程。你有没有考虑过为它实现一个委托?您可以发布一条消息,比如JSONRequestDidComplete,并将JSON字典作为参数一起传递。然后,您可以对视图控制器中设置为请求类的委托的字典执行所需的操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13763937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档