我有一些代码可以对基于 json 的 web 服务进行 http 调用。这工作正常,但我试图将代码移动到它自己的类,我有一点挂断。当我调用该方法时,主线程只是继续执行下一个命令,而无需等待我的方法的响应。
这是程序主要部分的一些代码
[newcall run];
NSLog(@"%@",[newcall status]);
NSArray *resultarray= [newcall returndata];
for (NSString *element in resultarray) {
NSLog(@"%@",element);
}
我的标题
#import "AFHTTPClient.h"
@interface jsoncall : AFHTTPClient
{
NSString* Date;
NSString* apps;
NSString* data1;
NSURL* url;
NSString* Path;
NSArray* returndata;
NSString* status;
}
-(void) setApp: (NSString *)input;
-(void) setData: (NSString *)input;
-(void) setURL: (NSString *)input;
-(void) setPath: (NSString *)input;
-(int) run;
-(NSArray *) returndata;
-(NSString *) status;
@end
我的运行方法
-(int) run
{
__block int success;
NSDictionary* jsonDictionary=[NSDictionary dictionaryWithObject: data1 forKey:@"data"];
NSString* jsonString = [jsonDictionary JSONRepresentation];
AFHTTPClient *httpClient=[[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params =[NSDictionary dictionaryWithObjectsAndKeys:
apps,@"app",
jsonString,@"smpdata",nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:Path parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSArray *dataarray=[JSON valueForKey:@"Data"];
status= [NSString stringWithFormat:@"%@",[JSON valueForKeyPath:@"Status"]];
NSLog(@"%@",status);
returndata= dataarray;
success=1;
NSLog(@"Success: Made it here");
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error,id JSON)
{
success=0;
NSLog(@"Error: Made it here");
}
];
[operation start];
[operation waitUntilFinished];
return success;
}