我已经实现了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];
}发布于 2012-12-07 22:56:58
根据其他用户的建议,我最终创建了自己的委托,它在success和failure方法中执行两个选择器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;
@endJSONRequest.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];
}@end
发布于 2012-12-07 21:37:33
您的AFJSONRequestOperation异步运行!所以它不是在主线程上运行(我认为这就是AFJSONRequestOperation的目的),但是您的+ (void)RequestJSON:(NSDictionary *)params:(NSString *)webServicePath方法是在主线程上执行的,因此会立即完成。例如,here's某人问同样的问题。
因为AFJSONRequestOperation是基于NSOperation的,所以可以将其添加到mainQueu中。但是,从主线程发送同步请求并不是一个好主意。但也许你可以看看this workaround。
发布于 2012-12-07 21:37:49
该函数返回是因为它的任务只是设置OperationQueue。一旦它这样做了,它就会像往常一样继续它的流程。你有没有考虑过为它实现一个委托?您可以发布一条消息,比如JSONRequestDidComplete,并将JSON字典作为参数一起传递。然后,您可以对视图控制器中设置为请求类的委托的字典执行所需的操作。
https://stackoverflow.com/questions/13763937
复制相似问题