sandeep tomar
sandeep tomar

Reputation: 305

how to communicate with server with afnetworking?

I am using AFNetworking Please take a look at my code

+(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"];

I am using this piece of code, but when i reach on this method it jump to failure block.

[afRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)

This block is not executed.

The program moves jump on this block

failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        newactivity.enabled = NO;
        if(del){
            [del isLoginRespomce:isLogin LoginDetail:user];
        }

Upvotes: 0

Views: 82

Answers (1)

Erik Terwan
Erik Terwan

Reputation: 2780

Your code could be much simpler, use for the AFNetworking stuff the AFHTTPRequestOperationManager, it's really easy to use and works just as good as allocating your own AFHTTPRequestOperation etc.

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);
}];

Upvotes: 2

Related Questions