AbhiRam
AbhiRam

Reputation: 2061

No response coming from server using NSUrlSession

Hi i am very new for ios and in my project i am integrating my app with services using NSurlSEssion but when i send request to server there is no response is coming from server please help me:-

my main class:-

 NSString * finalString = @"UserName=101229518299&Password=123456&grant_type=Password";

[post5 postServieCalling:@"%@myUrl" :finalString];

server background class:-

-(void)postServieCalling :(NSString*)mainurl :(NSString*)params{

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:mainurl,ServerBaseURL]]

                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy

                                                       timeoutInterval:60.0];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        dispatch_async(dispatch_get_main_queue(), ^{

        });

        if (error) {

            NSLog(@"dataTaskWithRequest error: %@", error);

            NSString * BasicnetworkError = [error localizedDescription];
            NSString * AppendString = @"Http Response failed with the following";
            NSString * networkError = [AppendString stringByAppendingString:BasicnetworkError];

            [self BasicError1:networkError];

        }

        else if ([response isKindOfClass:[NSHTTPURLResponse class]]) {

            NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

            if (statusCode != 200) {

                NSError *parseError;
                id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
                [self MainService:responseObject];

            }else{

                NSError *parseError;

                id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];

                NSLog(@"else condtion");

                if (!responseObject) {

                    NSLog(@"JSON parse error: %@", parseError);

                    NSLog(@"responseobject is%@",responseObject);

                } else {


                    NSLog(@"responseobject is %@",responseObject);

                    [self MainService:responseObject];
                }

                //if response was text/html, you might convert it to a string like so:

                NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                NSLog(@"final responseString = %@", responseString);
            }
        }
    }];

    [task resume];
}

Upvotes: 0

Views: 407

Answers (1)

Anil solanki
Anil solanki

Reputation: 962

I think you dont have included the app transport security in your app's info.plist please add that key and your code will run fine.

Upvotes: 1

Related Questions