我正在使用AFNetworking
请查看我的代码
+(void)requestLogin:(id<LoginRequestDelegate>) delegate LoginURL:(NSString *)loginurl{
static User *user;
static id <LoginRequestDelegate> del;
del=delegate;
static AFHTTPRequestOperation *afRequest;
if(afRequest){
return;
}
isLogin = NO;
Reachability *objInternetReachable = [Reachability reachabilityForInternetConnection];
NetworkStatus status = [objInternetReachable currentReachabilityStatus];
if(status != NotReachable) {
NSString *strUrl=loginurl;
NSLog(@"Login JSON URL %@", strUrl);
strUrl=[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *urlReq=[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:strUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
afRequest=[[AFHTTPRequestOperation alloc] initWithRequest:urlReq];
AFNetworkActivityIndicatorManager * newactivity = [[AFNetworkActivityIndicatorManager alloc] init];
newactivity.enabled = YES;
[afRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
newactivity.enabled = NO;
NSString *strRes=[[NSString alloc] initWithData:responseObject encoding:NSASCIIStringEncoding];
//JSON DATA
NSDictionary *dic=[[NSDictionary alloc]init];
SBJsonParser *parser=[[SBJsonParser alloc] init];
dic=[parser objectWithString:strRes];
NSLog(@"Dic :- %@", dic);
if(dic){
NSString *isLoginRes = [dic objectForKey:@"login"];
if([isLoginRes isEqualToString:@"true"]){
NSLog(@"login res %@",isLoginRes);
isLogin = YES;
NSDictionary *resp = [dic objectForKey:@"resp"];
user = [User sharedInstance];
// User
NSDictionary *userDic = [resp objectForKey:@"User"];
user.userId = [userDic objectForKey:@"id"];
user.userEmail = [userDic objectForKey:@"email"];
我正在使用这段代码,但是当我使用此方法时,它会跳到故障块。
[afRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
该块不执行。
程序在该块上移动跳转
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
newactivity.enabled = NO;
if(del){
[del isLoginRespomce:isLogin LoginDetail:user];
}
最佳答案
您的代码可能更简单,将AFccinging用作AFHTTPRequestOperationManager
,非常容易使用,并且与分配自己的AFHTTPRequestOperation
一样好。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:loginurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];