繁体   English   中英

使用NSUrlSession的服务器无响应

[英]No response coming from server using NSUrlSession

嗨,我对ios非常陌生,在我的项目中,我正在使用NSurlSEssion将我的应用程序与服务集成在一起,但是当我向服务器发送请求时,服务器没有响应,请帮助我:-

我的主要班级:

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

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

服务器后台类:-

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

我认为您没有在应用程序的info.plist中包含应用程序传输安全性,请添加该密钥,您的代码即可正常运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM